Skip to content

About

Scrape one practice site with eight Python libraries through IPRoyal residential or datacenter proxies, and compare both types on your own target.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Python Web Scraping Libraries With Proxies

IPRoyal residential proxies

tests

The same scrape in the eight Python web scraping libraries from IPRoyal's guide: Requests with Beautiful Soup, urllib3 with lxml, aiohttp, Scrapy, Selenium and Playwright. Each example reads the title, price, rating and stock status of the books on books.toscrape.com, a public practice site, and sends every request through a proxy.

The proxy settings work with IPRoyal residential proxies (one gateway, IPs rotated for you) and IPRoyal datacenter proxies (a list of IPs, rotated in your code). A comparison script runs the same small scrape through both, so you can measure which one your target needs.

This repo goes with the guide 8 Best Python Web Scraping Libraries in 2026.

Quick start

git clone https://github.com/IPRoyal/python-web-scraping-libraries.git
cd python-web-scraping-libraries
pip install -r requirements.txt
cp .env.example .env        # then add your proxy username and password
python examples/requests_bs4.py --pages 2
Proxy: http://your-username:***@geo.iproyal.com:12321
Scraped 40 books from 2 page(s):
  £ 51.77  3/5  in stock      A Light in the Attic
  £ 53.74  1/5  in stock      Tipping the Velvet
  ...

Python 3.10 or newer. For Selenium and Playwright, also run pip install -r requirements-browsers.txt and playwright install chromium. Selenium uses your installed Chrome.

Which proxy this uses and why

Every example takes --proxy-type residential (the default) or --proxy-type datacenter. Both are set in .env.

Residential proxies go through one gateway, geo.iproyal.com. Options go in the password (IPRoyal docs):

  • Rotation: a new IP with each request, the default
  • Sticky sessions: one IP kept for anything from 1 second to 7 days
  • Targeting: country, US state or city

Datacenter proxies are bought per IP. The dashboard's Formatted Proxy List gives each IP its own host, port and credentials (IPRoyal docs). You pick the country, and optionally the state or city, when you order.

There's no gateway, so rotating across the list happens in your code: DatacenterRotator in proxy_config.py does round robin or random picks, moves on when a request fails and drops an IP after three failures in a row.

How each one is sold

From iproyal.com's pricing and product pages, read on 21 November 2026:

Datacenter Residential
What you buy IPs, for 30, 60 or 90 days Traffic, per GB
Price Per proxy; the price per proxy drops for 60- and 90-day periods Per GB; the price per GB drops for larger amounts, and subscriptions cost less than Pay As You Go
Traffic 100 GB per IP per monthly cycle, pooled across the order. After that the speed is reduced, and more traffic can be bought in the dashboard Bought traffic doesn't expire
IPs The IPs in your order, rotated by your code A new IP per request, or a sticky session
Location Chosen per order: country, then state or city Set per request in the password

Current prices: residential, datacenter.

Measure it on your target

Whether the per-IP option is enough depends on the site you scrape, so measure it. scripts/compare_proxy_types.py sends the same requests through both types, alternating URL by URL, and prints one row per type:

python scripts/compare_proxy_types.py                       # 5 books.toscrape.com pages per type
python scripts/compare_proxy_types.py --urls https://example.com/a https://example.com/b --expect "<title>" --rounds 3
python scripts/compare_proxy_types.py --csv results.csv     # every request, for your own analysis
  • success: the share of requests that got HTTP 200 and, with --expect, contained that text
  • median time: from sending the request to the last byte, successful requests only
  • bytes total and KB/request: status line, headers and body as received (compressed size when the site compresses), failed requests included. Multiply KB/request by your planned request count to estimate traffic; your IPRoyal dashboard shows the billed figure.

Point it at a site only within that site's terms, and keep the run small.

Examples

Library How it takes the proxy Parser Example
Requests + Beautiful Soup proxies={"http": url, "https": url} CSS selectors requests_bs4.py
urllib3 + lxml ProxyManager(server, proxy_headers=make_headers(proxy_basic_auth=...)) XPath urllib3_lxml.py
aiohttp session.get(url, proxy=url), a different proxy per request if you like lxml aiohttp_concurrent.py
Scrapy request.meta["proxy"], set by a downloader middleware Scrapy selectors scrapy_spider.py
Selenium --proxy-server pointing at a local relay that adds the credentials find_elements selenium_example.py
Playwright launch(proxy={"server", "username", "password"}) Locators playwright_example.py

Plus datacenter_rotation.py, which prints the IP each page went through.

Things that trip people up, each one covered by a test:

  • books.toscrape.com sends no charset. Its Content-Type is plain text/html, so Requests decodes response.text as ISO-8859-1 and £51.77 turns into £51.77. The examples pass response.content (bytes) to the parser, which reads the UTF-8 charset from the page's <meta> tag.

  • urllib3 ignores credentials in the proxy URL and gets a 407. Send them in the Proxy-Authorization header, as the example does.

  • Chrome's --proxy-server has no place for a username and password. proxy_relay.py listens on 127.0.0.1 without a password and forwards to the proxy with your credentials, for HTTPS (CONNECT) and plain HTTP.

    In our tests with Selenium 4.53 and Chrome 157, Selenium's BiDi network.add_auth_handler didn't answer the proxy's 407, which is why the example uses the relay.

  • Scrapy fetches robots.txt before the first page. The middleware sets the proxy on that request too, so nothing goes out without it.

  • Browsers download more than the page. In the offline tests, one listing page in Playwright sent about 30 requests through the proxy (the HTML plus its images, CSS and scripts). Requests, urllib3 and aiohttp fetch only the HTML; Scrapy adds robots.txt.

    Chrome under Selenium also sent some of its own requests to Google hosts through the proxy. With per-GB traffic, run compare_proxy_types.py or check the dashboard before scaling up a browser scrape.

Rotating datacenter IPs

from proxy_config import DatacenterRotator, parse_datacenter_proxies

proxies = parse_datacenter_proxies("45.12.106.20:12323:user:pass, 45.12.106.21:12323:user:pass")
rotator = DatacenterRotator(proxies, strategy="round_robin", max_failures=3)

proxy = rotator.next()            # http://user:pass@45.12.106.20:12323
# ... send the request ...
rotator.report_success(proxy)     # or rotator.report_failure(proxy) on an error, 403, 407 or 429

IPROYAL_DATACENTER_PROXIES takes the list comma-separated or as a path to a file with one proxy per line. Each line can be host:port:user:pass, user:pass:host:port, user:pass@host:port, a full http:// URL, or host:port for IPs you whitelisted in the dashboard.

try_proxies(source, call) wraps the retry loop: it backs off, moves to the next IP and raises the last error after three tries.

For residential proxies, next() returns the same gateway URL each time, because the gateway picks the IP. The examples call the same methods for both types.

Using another proxy

Set PROXY_URL in .env and every example uses it instead of the IPRoyal settings:

PROXY_URL=http://user:pass@proxy.example.com:8080

compare_proxy_types.py and live_check.py ignore PROXY_URL, because they compare or check the two IPRoyal types.

The examples are written and tested for HTTP proxies. SOCKS5 support differs per library (Requests needs pip install "requests[socks]", for example), so check the library's docs before you switch.

Tests

pip install -r requirements-dev.txt
playwright install chromium
pytest

The offline tests need no proxy account and no network. A local mock proxy asks for credentials, records what each library sends, and answers with real captures of two books.toscrape.com pages (tests/fixtures). The tests check that:

  • each library's parser reads the same 40 books from the captures
  • each library sends its requests through the proxy with the right username and password, Selenium and Playwright included (in headless browsers)
  • the datacenter list parses in every accepted format, rotates, and drops failing IPs
  • the comparison script's success rate, median and byte counts are right on canned results

The mock proxy doesn't do HTTPS, so the offline tests use http:// URLs. Browser tests skip if Chrome or Playwright's Chromium is missing, except in CI. They run on every push (test.yml).

The live check scrapes books.toscrape.com through IPRoyal with each library (about 20 small requests). With IPROYAL_DATACENTER_PROXIES set, it also checks that the first IPs in the list exit from different addresses and runs a short comparison:

python scripts/live_check.py --browsers

On GitHub it runs by hand from the Actions tab (live-check.yml) with the IPROYAL_USERNAME, IPROYAL_PASSWORD and, optionally, IPROYAL_DATACENTER_PROXIES secrets.

Project layout

proxy_config.py                  residential URL, datacenter list, rotation (IPRoyal options or PROXY_URL)
proxy_relay.py                   local relay that adds proxy credentials for Chrome
books_site.py                    the practice site's URLs and the Book record
examples/                        one script per library, plus datacenter rotation
scripts/compare_proxy_types.py   same scrape through datacenter and residential, side by side
scripts/live_check.py            live run through IPRoyal
tests/                           offline tests with a local mock proxy and captured pages

Read the full guide

The guide explains what each library is good at and how to combine an HTTP client, a parser and a browser tool: 8 Best Python Web Scraping Libraries in 2026.

License

MIT

About

Scrape one practice site with eight Python libraries through IPRoyal residential or datacenter proxies, and compare both types on your own target.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages