我每天都在使用的5个Python自动化脚本

TL;DR- 我日常使用的最好的Python脚本的快速列表,加上一些可能的修改,可以用于其他功能。

简介

Python是一种强大的编程语言,可以用来实现各种任务的自动化。无论你是在开发一个小项目还是一个大型企业应用,Python都可以帮助你节省时间并简化工作流程。

Python是一种伟大的语言,因为它的语法简单得令人难以置信。一个体面的程序员用Python的10行代码就能完成的事情,在Javascript或C++这样的语言中需要20行。下面是一个简单的网络请求的例子→

import requests 
r = requests.get("https://www.python.org") 
print(r.status_code)
print(r.text)

相比之下,这里的Javascript代码做了完全相同的事情→。

fetch("https://www.python.org")
.then(res => {
 if(res.ok) {
 return res.text();
 } else {
 throw new Error("HTTP error, status = " + res.status);
 }
})
.then(text => {
 console.log(text);
})
.catch(error => {
 console.log(error);
});

正如你所看到的,Python代码远比Javascript代码更容易消化,这使得它非常适合用于自动化重复性任务,如网络刮削、数据收集或翻译。下面是我最喜欢的五项用Python实现自动化的重复性任务→。

A URL Shortener →

import pyshorteners
s = pyshorteners.Shortener(api_key="YOUR_KEY")
long_url = input("Enter the URL to shorten: ")
short_url = s.bitly.short(long_url)
print("The shortened URL is: " + short_url)

在谈到URL缩短时,Pyshorteners库是我的最爱之一,它可以用于各种项目。对于大多数链接缩短器,你需要一个API密钥,但它们通常是免费的,除非你预计会有数十万个请求。我发现像Bit.ly、Adf.ly和Tinyurl这样的API对于像SaaS应用和Telegram机器人这样的东西是非常好的。

Creating Fake Information →

import pandas as pd
from faker import Faker
# Create object
fake = Faker()
# Generate data
fake.name()
fake.text()
fake.address()
fake.email()
fake.date()
fake.country()
fake.phone_number()
fake.random_number(digits=5)
# Dataframe creation
fakeDataframe = pd.DataFrame({'date':[fake.date() for i in range(5)],
 'name':[fake.name() for i in range(5)],
 'email':[fake.email() for i in range(5)],
 'text':[fake.text() for i in range(5)]})
print(fakeDataframe)

如果你需要创建一个假人,这个faker库为你提供了一个Faker类,可以自动生成一个完整的人。这个脚本创建了几个不同的人,并将他们存储在一个数据框架中,这是一个稍微复杂的概念。如果我需要向一些可疑的网站提供信息,或者我不希望任何东西被追踪到我身上,我就会使用这些假人。

Youtube Video Downloader →

from pytube import YouTube
link = input("Enter a youtube video's URL") # i.e. https://youtu.be/dQw4w9WgXcQ
yt = Youtube(link)
yt.streams.first().download()
print("downloaded", link)

很简单。它使用pytube库将你提供的任何链接转换为文件,然后下载它。只有五行代码,没有API速率限制,你可以把它与另一个脚本结合起来,转录视频,并使用情感分析来确定视频包含什么样的内容。

NATO Phonetic Alphabet Encryptor

def encrypt_message(message):
 nato_alphabet = {
 'A': 'Alfa', 'B': 'Bravo', 'C': 'Charlie', 'D': 'Delta',
 'E': 'Echo', 'F': 'Foxtrot', 'G': 'Golf', 'H': 'Hotel',
 'I': 'India', 'J': 'Juliet', 'K': 'Kilo', 'L': 'Lima',
 'M': 'Mike', 'N': 'November', 'O': 'Oscar', 'P': 'Papa',
 'Q': 'Quebec', 'R': 'Romeo', 'S': 'Sierra', 'T': 'Tango',
 'U': 'Uniform', 'V': 'Victor', 'W': 'Whiskey', 'X': 'Xray',
 'Y': 'Yankee', 'Z': 'Zulu'
 }
encrypted_message = ""
 
# Iterate through each letter in the message
 for letter in message:
 
 # If the letter is in the dictionary, add the corresponding codeword to the encrypted message
 if letter.upper() in nato_alphabet:
 encrypted_message += nato_alphabet[letter.upper()] + " "
 
 # If the letter is not in the dictionary, add the original letter to the encrypted message
 else:
 encrypted_message += letter
return encrypted_message
message = "Hello World"
encrypted_message = encrypt_message(message)
print("Encrypted message: ", encrypted_message)

这个函数对传入其输入参数的任何信息进行编码,并输出相应的NATO字串。这个函数能正常工作,因为它检查每个字符是否在nato_alphabet字典中,如果是,它将被附加到加密的消息中。如果该字符在字典中找不到(如果是空格、冒号或任何不属于a-z的字符),它将被附加,没有任何特殊编码。因此,”Hello World “变成 “Hotel Echo Lima Lima Oscar””Whiskey Oscar Romeo Lima Delta”。

Social Media Login Automation

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.facebook.com/")
# Find the email or phone field and enter the email or phone number
email_field = driver.find_element_by_id("email")
email_field.send_keys("your_email_or_phone")
# Find the password field and enter the password
password_field = driver.find_element_by_id("pass")
password_field.send_keys("your_password")
# Find the login button and click it
login_button = driver.find_element_by_id("loginbutton")
login_button.click()

这段代码利用了Selenium,一个用于网络自动化的流行库。它打开一个网络浏览器,根据代码中给出的各种命令进行导航。在这个特定的代码块中,浏览器将进入Facebook,并在网页上找到要修改的特定元素。在这里,我们在电子邮件和密码字段中输入某些按键,并点击 “登录 “按钮。如果提供了有效的凭证,这将自动登录一个用户。

猜你喜欢

转载自blog.csdn.net/jeansboy/article/details/131707196