DEV Community

Xinglin Ming
Xinglin Ming

Posted on

Build a Web Scraper in Python in 10 Minutes

Web Scraping Made Simple

import requests
from bs4 import BeautifulSoup
import csv

def scrape(url, selector):
    r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
    soup = BeautifulSoup(r.text, 'html.parser')
    elements = soup.select(selector)
    return [e.get_text(strip=True) for e in elements]

data = scrape('https://example.com', 'h1')
print(data)
Enter fullscreen mode Exit fullscreen mode

Need the Full Scraper Pro Toolkit?

My Web Scraper Pro includes multi-page scraping, proxy support, and CSV/JSON export.

Custom scripts available: mactavish.ming@gmail.com

Top comments (0)