5 Python automation scripts I use every day

introduce

Python is a powerful programming language that can be used to automate various tasks. Whether you're developing a small project or a large enterprise application, Python can help you save time and streamline your workflow.

Python is a great language because of its very simple syntax. What is done in 10 lines of Python code, would take 20 lines of code in a language like Javascript or C++. Here is an example of a simple web request:

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

Below is the Javascript code to accomplish the same function:​​​​​​​​

 
 

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);});

As you can see, Python code is easier to understand than Javascript code, making it ideal for automating repetitive tasks such as web scraping, data collection, or translation. Here are five of the repetitive tasks I do most often, and I do them in Python.

URL shortener

 
 

importpyshorteners
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)

The Pyshorteners library is one of my favorites when it comes to URL shortening and can be used for a variety of projects. Most link shorteners require an API key, but unless you expect hundreds of thousands of requests, they're usually free. I've found that APIs like Bit.ly, Adf.ly, and Tinyurl are great for SaaS apps and Telegram bots.

Create fake information

 
 

import pandas as pdfrom faker import Faker
# Create objectfake = Faker()
# Generate datafake.name()fake.text()fake.address()fake.email()fake.date()fake.country()fake.phone_number()fake.random_number(digits=5)
# Dataframe creationfakeDataframe = 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)

If you need to create a dummy (fake character), this FakeLib provides you with a Fake class that automatically generates the whole dummy. This script creates several different people and stores them in a data frame, which is a slightly more complicated concept. I use these dummy info if I have to give information to less trustworthy sites, or if I don't want anyone else to be able to trace any information back to me.

Youku 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)

very simple. It uses the pytube library to convert any link you give it to a file, then downloads it. With five lines of code and no API rate limiting, you can combine it with another script to transcribe a video and use sentiment analysis to determine what type of content the video contains.

NATO Phonetic 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)

This function encodes any message passed to its input parameter and outputs the corresponding sequence of NATO words. This works fine because it checks each character if it is in the nato_alphabet dictionary, and if so, it is appended to the encrypted message. If the character is not found in the dictionary (if it is a space, colon, or anything that is not a-z ), it is appended without any special encoding. So "Hello World" becomes "Echo Hotel Lima Oscar Lima" "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 numberemail_field = driver.find_element_by_id(“email”)email_field.send_keys(“your_email_or_phone”)
# Find the password field and enter the passwordpassword_field = driver.find_element_by_id(“pass”)password_field.send_keys(“your_password”)
# Find the login button and click itlogin_button = driver.find_element_by_id(“loginbutton”)login_button.click()

This code utilizes Selenium, a popular web automation library. It opens a web browser and navigates according to various commands given in the code. In this particular block of code, the browser will redirect to Facebook and find the specific element on the page to modify. Here we enter certain characters in the email and password fields and click on the "Login" button. This will automatically log the user in if valid credentials are provided.

Guess you like

Origin blog.csdn.net/y1282037271/article/details/129409707