183. Web Scraping

Snippet 1: Basic Web Scraping with BeautifulSoup

from bs4 import BeautifulSoup
import requests

url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")

title = soup.find("title").text
print(f"Page Title: {title}")

Snippet 2: Extracting All Links from a Web Page

from bs4 import BeautifulSoup
import requests

url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")

links = [a['href'] for a in soup.find_all('a', href=True)]
print("Links found:", links)

Snippet 3: Scraping Table Data with BeautifulSoup


Snippet 4: Using Scrapy Shell to Inspect a Web Page

In the Scrapy shell:


Snippet 5: Basic Scraper with Scrapy


Snippet 6: Scraping Data into a JSON File with Scrapy


Snippet 7: Scraping Images with BeautifulSoup


Snippet 8: Handling Pagination with Scrapy


Snippet 9: Extracting Metadata with BeautifulSoup


Snippet 10: Scraping JSON Data with Requests


Last updated