|
2 | 2 | """ |
3 | 3 | AutoScraper with proxy headers example. |
4 | 4 |
|
5 | | -This example shows how to use AutoScraper with custom proxy headers. |
6 | | -AutoScraper automatically learns scraping rules while python-proxy-headers |
7 | | -enables sending custom headers to the proxy server. |
| 5 | +Configuration via environment variables: |
| 6 | + PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080 |
| 7 | + TEST_URL - URL to request (default: https://api.ipify.org?format=json) |
| 8 | + PROXY_HEADER - Header name to send to proxy (optional) |
| 9 | + PROXY_VALUE - Header value to send to proxy (optional) |
| 10 | + RESPONSE_HEADER - Header name to read from response (optional) |
8 | 11 |
|
9 | 12 | See: https://github.com/proxymesh/python-proxy-headers |
10 | | -Docs: https://python-proxy-headers.readthedocs.io/en/latest/autoscraper.html |
11 | 13 | """ |
| 14 | +import os |
| 15 | +import sys |
12 | 16 | from python_proxy_headers.autoscraper_proxy import ProxyAutoScraper |
13 | 17 |
|
14 | | -# Create an AutoScraper with proxy header support |
15 | | -scraper = ProxyAutoScraper(proxy_headers={'X-ProxyMesh-Country': 'US'}) |
16 | | - |
17 | | -# Proxy configuration |
18 | | -proxies = { |
19 | | - 'http': 'http://USERNAME:PASSWORD@PROXYHOST:PORT', |
20 | | - 'https': 'http://USERNAME:PASSWORD@PROXYHOST:PORT' |
21 | | -} |
22 | | - |
23 | | -# Build scraping rules from a sample page |
24 | | -# AutoScraper learns what to extract based on the wanted_list |
25 | | -result = scraper.build( |
26 | | - url='https://quotes.toscrape.com/', |
27 | | - wanted_list=['The world as we have created it is a process of our thinking.'], |
28 | | - request_args={'proxies': proxies} |
29 | | -) |
30 | | - |
31 | | -print(f"Found {len(result)} matching quotes") |
32 | | -for quote in result[:5]: |
33 | | - print(f" - {quote[:60]}...") |
34 | | - |
35 | | -# Save learned rules for later use |
36 | | -# scraper.save('quotes_rules.json') |
37 | | - |
38 | | -# Use learned rules on another page |
39 | | -# result = scraper.get_result_similar( |
40 | | -# url='https://quotes.toscrape.com/page/2/', |
41 | | -# request_args={'proxies': proxies} |
42 | | -# ) |
| 18 | +# Get configuration from environment |
| 19 | +proxy_url = os.environ.get('PROXY_URL') or os.environ.get('HTTPS_PROXY') |
| 20 | +if not proxy_url: |
| 21 | + print("Error: Set PROXY_URL environment variable", file=sys.stderr) |
| 22 | + sys.exit(1) |
| 23 | + |
| 24 | +test_url = os.environ.get('TEST_URL', 'https://api.ipify.org?format=json') |
| 25 | +proxy_header = os.environ.get('PROXY_HEADER') |
| 26 | +proxy_value = os.environ.get('PROXY_VALUE') |
| 27 | +response_header = os.environ.get('RESPONSE_HEADER') |
| 28 | + |
| 29 | +proxy_headers = {proxy_header: proxy_value} if proxy_header and proxy_value else None |
| 30 | + |
| 31 | +# Create scraper and test via underlying session |
| 32 | +scraper = ProxyAutoScraper(proxy_headers=proxy_headers) |
| 33 | +session = scraper._get_session() |
| 34 | +session.proxies = {'http': proxy_url, 'https': proxy_url} |
| 35 | + |
| 36 | +# Make request |
| 37 | +response = session.get(test_url) |
| 38 | + |
| 39 | +# Output |
| 40 | +print(f"Status: {response.status_code}") |
| 41 | +print(f"Body: {response.text}") |
| 42 | +if response_header: |
| 43 | + print(f"{response_header}: {response.headers.get(response_header)}") |
43 | 44 |
|
44 | 45 | scraper.close() |
0 commit comments