Web scraping and browser automation are essential for data extraction, testing, and automating manual web-based workflows. Python offers three widely-used tools for such tasks: Requests for sending HTTP requests, Beautiful Soup for parsing HTML, and Selenium for automating web browsers.
In this document, we explore working code samples that demonstrate how to use these libraries effectively. Each section includes clear examples and explanations for tasks like fetching content, parsing HTML, navigating DOM trees, and automating browser interaction using Selenium WebDriver. These practical examples will provide a strong foundation for scraping and automation projects.
pip install requests
import requests
url = "https://www.example.com"
response = requests.get(url)
print("Status Code:", response.status_code)
print("Headers:", response.headers)
print("Content:", response.text[:500]) # Print first 500 characters
url = "https://httpbin.org/post"
data = {"username": "test", "password": "123456"}
response = requests.post(url, data=data)
print("POST Response:", response.json())
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
}
response = requests.get("https://httpbin.org/headers", headers=headers)
print(response.json())
try:
response = requests.get("https://www.example.com", timeout=5)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
print("HTTP error:", e)
except requests.exceptions.Timeout:
print("Request timed out")
except requests.exceptions.RequestException as e:
print("Error:", e)
img_url = "https://www.example.com/logo.png"
response = requests.get(img_url)
with open("logo.png", "wb") as f:
f.write(response.content)
pip install beautifulsoup4
pip install lxml
from bs4 import BeautifulSoup
import requests
url = "https://www.example.com"
html = requests.get(url).text
soup = BeautifulSoup(html, "lxml")
print(soup.title.text)
print(soup.find("h1"))
print(soup.find_all("a"))
for link in soup.find_all("a"):
print(link.get("href"))
for item in soup.select("div.content > ul > li"):
print(item.text)
html = """
Name Age
Alice 30
Bob 25
"""
soup = BeautifulSoup(html, "lxml")
rows = soup.find_all("tr")
for row in rows:
cols = row.find_all(["td", "th"])
print([col.text for col in cols])
# Removing script and style tags
for tag in soup(["script", "style"]):
tag.decompose()
print(soup.get_text())
url = "https://www.bbc.com/news"
html = requests.get(url).text
soup = BeautifulSoup(html, "lxml")
for headline in soup.select("h3"):
print(headline.text.strip())
pip install selenium
# Download ChromeDriver from https://chromedriver.chromium.org
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.example.com")
print(driver.title)
driver.quit()
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.google.com")
search_box = driver.find_element(By.NAME, "q")
search_box.send_keys("Selenium Python")
search_box.submit()
print(driver.title)
driver.quit()
button = driver.find_element(By.ID, "submit")
button.click()
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, "content")))
print(element.text)
driver.get("https://quotes.toscrape.com")
quotes = driver.find_elements(By.CLASS_NAME, "text")
for q in quotes:
print(q.text)
driver.quit()
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.example.com")
print(driver.title)
driver.quit()
while True:
quotes = driver.find_elements(By.CLASS_NAME, "text")
for quote in quotes:
print(quote.text)
try:
next_button = driver.find_element(By.CSS_SELECTOR, ".next > a")
next_button.click()
except:
break
driver.get("https://www.example.com/js-page")
content = driver.find_element(By.ID, "dynamic-content").text
print(content)
# First try with Requests + BS4
try:
response = requests.get("https://quotes.toscrape.com")
soup = BeautifulSoup(response.text, "lxml")
quotes = [q.text for q in soup.select(".quote .text")]
for q in quotes:
print(q)
except:
# If fails, use Selenium
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://quotes.toscrape.com")
quotes = driver.find_elements(By.CLASS_NAME, "text")
for q in quotes:
print(q.text)
driver.quit()
# Load with Selenium
driver.get("https://www.worldometers.info/world-population/population-by-country/")
# Extract table rows
rows = driver.find_elements(By.XPATH, "//table[@id='example2']/tbody/tr")
for row in rows[:5]: # first 5 countries
cols = row.find_elements(By.TAG_NAME, "td")
print([col.text for col in cols])
driver.quit()
This document showcased the combined power of three essential Python libraries for web data handling:
When building robust scraping pipelines, itβs common to start with Requests and Beautiful Soup for efficiency, and only fall back to Selenium when dynamic JavaScript content or interaction is required. Understanding when and how to use these tools allows you to extract data effectively while balancing performance and complexity.
Whether you are building data ingestion pipelines, testing workflows, or creating bots, mastering these libraries will significantly enhance your Python web automation toolkit.
Python is commonly used for developing websites and software, task automation, data analysis, and data visualisation. Since it's relatively easy to learn, Python has been adopted by many non-programmers, such as accountants and scientists, for a variety of everyday tasks, like organising finances.
Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works.
The point is that Java is more complicated to learn than Python. It doesn't matter the order. You will have to do some things in Java that you don't in Python. The general programming skills you learn from using either language will transfer to another.
Read on for tips on how to maximize your learning. In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.
6 Top Tips for Learning Python
The following is a step-by-step guide for beginners interested in learning Python using Windows.
Best YouTube Channels to Learn Python
Write your first Python programStart by writing a simple Python program, such as a classic "Hello, World!" script. This process will help you understand the syntax and structure of Python code.
The average salary for Python Developer is βΉ5,55,000 per year in the India. The average additional cash compensation for a Python Developer is within a range from βΉ3,000 - βΉ1,20,000.
Copyrights © 2024 letsupdateskills All rights reserved