Python Automation Scripts: Web Requests, URL Shortening, Fake Data Generation, Video Downloading, NATO Encryption, and Selenium Login
This article presents a series of Python automation examples—including HTTP requests, URL shortening, fake data creation, YouTube video downloading, NATO‑style message encryption, and Selenium‑driven social‑media login—showcasing concise code snippets and explanations that highlight Python's simplicity and versatility for repetitive tasks.
Python is a powerful, easy‑to‑learn language that can automate a wide range of tasks, from simple web requests to complex data generation and browser automation.
Simple web request example in Python:
import requests
r = requests.get("https://www.python.org")
print(r.status_code)
print(r.text)For comparison, the equivalent JavaScript using fetch is:
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);
});URL shortener using the pyshorteners library:
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)Generating fake personal data with Faker and pandas :
import pandas as pd
from faker import Faker
fake = Faker()
fakeDataframe = pd.DataFrame({
'date': [fake.date() for _ in range(5)],
'name': [fake.name() for _ in range(5)],
'email': [fake.email() for _ in range(5)],
'text': [fake.text() for _ in range(5)]
})
print(fakeDataframe)YouTube video downloader using pytube :
from pytube import YouTube
link = input("Enter a YouTube video's URL: ")
yt = YouTube(link)
yt.streams.first().download()
print("downloaded", link)NATO alphabet encryptor function:
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 = ""
for letter in message:
if letter.upper() in nato_alphabet:
encrypted_message += nato_alphabet[letter.upper()] + " "
else:
encrypted_message += letter
return encrypted_message
message = "Hello World"
encrypted_message = encrypt_message(message)
print("Encrypted message:", encrypted_message)Automating a Facebook login with Selenium:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.facebook.com/")
email_field = driver.find_element_by_id("email")
email_field.send_keys("your_email_or_phone")
password_field = driver.find_element_by_id("pass")
password_field.send_keys("your_password")
login_button = driver.find_element_by_id("loginbutton")
login_button.click()These snippets demonstrate how Python’s concise syntax makes it ideal for automating repetitive tasks such as web scraping, data collection, media downloading, simple encryption, and browser interaction.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.