Python Web Scraping: How to Use Requests, Parsing and Rotating Proxies?

Python Web Scraping: How to Use Requests, Parsing and Rotating Proxies?

cheap rotating proxies

Python web scraping at scale needs addresses as much as code: route the scraper through a private proxy pool and spread requests across thousands of IPs.

Python web scraping turns into an address problem after the first few hundred pages. Sites count requests per IP, answer with 429 or a captcha, and a script that ran fine in testing starts to return empty fields.

What is web scraping in Python?

It is a script that downloads pages, pulls the data you need out of the HTML and saves it to a table or a database. A working scraper fits in twenty lines: requests fetches the page, lxml or BeautifulSoup parses it, and the csv module writes the result.

How does web scraping work?

Every scraper repeats one loop. It sends an HTTP request, gets HTML back, finds elements by CSS selectors or XPath and extracts text or attributes. Then it takes the next URL from a list, a sitemap or the links on the page it just parsed.

How to use Selenium for web scraping?

Only where a page builds its content with JavaScript, because a browser driven by Selenium or Playwright costs far more memory and time per page. Most sites still deliver their main content in the first HTML response. Open the page source with Ctrl+U and search for a value you want. If it is there, requests will get it. If it appears only in the Elements panel of the developer tools, the data often comes from a JSON endpoint that you can call directly, which beats any browser on speed.

Which libraries does web scraping Python code need?

Three are enough for a first scraper. requests handles the network, lxml parses HTML fast and supports XPath, and concurrent.futures from the standard library runs downloads in parallel.

Rotating proxies python: how do threads share one list?

The script below loads a proxy list from a link, spreads 50 threads across it and prints the H1 of every page:

import requests
from lxml import html
from concurrent.futures import ThreadPoolExecutor

LIST_URL = "your list link from the dashboard"
proxies = requests.get(LIST_URL, timeout=20).text.split()

def fetch(i, url):
    p = proxies[i % len(proxies)]
    r = requests.get(url, timeout=15,
                     proxies={"http": "http://" + p, "https": "http://" + p},
                     headers={"User-Agent": "Mozilla/5.0"})
    return url, html.fromstring(r.content).xpath("//h1/text()")

with ThreadPoolExecutor(max_workers=50) as pool:
    for url, h1 in pool.map(fetch, range(len(urls)), urls):
        print(url, h1)

Each thread takes the next address, so the load on any single IP stays small. With 3,000 addresses in the list and 50 requests a second, every IP gets roughly one request a minute. For rotating proxies python scripts like this one, a list behind a link matters as much as the size of the pool: the script reloads it, and dead addresses drop out with no change on your side.

How do proxies give web scraping anonymity?

Most sites cap how many requests one address may send in a short window, and the answer is a 429, a captcha page or a silent block where the page arrives without the data. A pool spreads requests over many addresses and subnets, and since sites often limit whole subnets, thousands of networks hold up better than the same count of addresses packed into a few ranges. For HTTPS targets the proxy only opens a tunnel, so page content stays encrypted between the script and the site.

A session that holds cookies stays on one address until it ends, while catalogs, search results and price lists can rotate on every request.

What are web scraping best practices for long jobs?

Set a timeout of 10 to 20 seconds on every request, or one hung connection blocks a thread for good. Retry a failed request through a different address two or three times at most and log the URL when every attempt fails. Save results after each page, so a crash at page 80,000 costs minutes. A random pause of one or two seconds per thread turns a burst into a steady stream. When the share of empty results climbs above a few percent, the site has changed its layout or started to block, and the fix is new selectors or more addresses in rotation.

How to learn web scraping?

Start with books.toscrape.com and quotes.toscrape.com, which Zyte publishes for training scrapers. Then add pagination, threads and proxies in that order, testing each step on one page, then ten, then a thousand. Scrapy comes next once you want queues, retries and export pipelines out of the box.

Which private proxies for data scraping?

WinGate gives access to a private IPv4 pool where 12,000 to 15,000 addresses are online during the day across 3,000 to 5,000 networks, and the list refreshes every 15 minutes. One access serves HTTP, HTTPS and SOCKS5, plans are counted in threads at 1,000, 3,000 or 5,000, and traffic is not metered. Up to two of your IPs are bound in the dashboard. The free test lasts up to 2 hours, long enough to point a real scraper at the list and compare its error rate with what you get today. For hobby scripts and one-off jobs, the daily plan is the cheap rotating proxies option.

Report Page