How to Scrape Free Proxy Lists in Python and Test Them Live
Introduction
Scraping free proxy lists and testing them can be an essential part of web scraping and automation tasks. However, relying on free proxies comes with its own set of challenges, including reliability, anonymity, and potential for IP bans. In this guide, we'll walk you through building a Python script to scrape proxy lists from various sources using requests and BeautifulSoup, and then test these proxies asynchronously to ensure they are functional.
Setting Up Your Environment
Before diving into the code, make sure you have the necessary Python libraries installed. You can install them using pip:
pip install requests beautifulsoup4 aiohttp
- Requests: For making HTTP requests.
- BeautifulSoup4: For parsing HTML content.
- Aiohttp: For asynchronous HTTP requests during proxy testing.
Scraping Free Proxy Lists
We'll start by scraping a popular free proxy list. Let's use FreeProxyList.net as an example. This site provides a table of proxies that you can scrape using requests and BeautifulSoup.
import requests
from bs4 import BeautifulSoup
def scrape_proxies(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', {'id': 'proxylisttable'})
proxies = []
for row in table.tbody.find_all('tr'):
cols = row.find_all('td')
ip = cols[0].text.strip()
port = cols[1].text.strip()
proxies.append(f'{ip}:{port}')
return proxies
# Usage
proxy_url = 'https://free-proxy-list.net/'
proxies_list = scrape_proxies(proxy_url)
print(proxies_list[:10]) # Print first 10 proxies for verification
Notes:
- The HTML structure of the page can change, so you may need to adjust the
findandfind_allmethods accordingly. - Always check the site's terms of service regarding scraping.
Testing Proxies Asynchronously
Now that we have a list of proxies, let's test them asynchronously using aiohttp. This will allow us to check multiple proxies concurrently, speeding up the testing process significantly.
import aiohttp
import asyncio
async def test_proxy(session, proxy):
try:
async with session.get('https://httpbin.org/ip', proxy=f'http://{proxy}', timeout=5) as response:
if response.status == 200:
return {'proxy': proxy, 'status': 'alive'}
except Exception as e:
return {'proxy': proxy, 'status': str(e)}
async def test_proxies(proxies):
async with aiohttp.ClientSession() as session:
tasks = [test_proxy(session, proxy) for proxy in proxies]
results = await asyncio.gather(*tasks)
return [result for result in results if result['status'] == 'alive']
# Usage
loop = asyncio.get_event_loop()
alive_proxies = loop.run_until_complete(test_proxies(proxies_list))
print(alive_proxies[:10]) # Print first 10 alive proxies for verification
Notes:
- We're using
httpbin.org/ipto test the proxy. This service returns your public IP address, which helps verify that the proxy is working. - The timeout is set to 5 seconds to prevent the testing process from taking too long.
Saving Proxies to JSON
Finally, let's save the list of tested and alive proxies to a JSON file for easy access in future scripts or applications.
import json
def save_proxies_to_json(proxies, filename='alive_proxies.json'):
with open(filename, 'w') as f:
json.dump(proxies, f, indent=4)
# Usage
save_proxies_to_json(alive_proxies)
Notes:
- The JSON file will be saved in the same directory as your script.
- You can load this JSON file later using
json.load().
Conclusion
Building a proxy scraper and tester from scratch is a great way to understand the mechanics behind managing proxies for web scraping tasks. However, manually managing proxy lists can become cumbersome and time-consuming, especially when you need reliable proxies for large-scale automation or web scraping projects.
If you'd rather not manage proxy lists manually, ProxyForge auto-tests and rotates proxies locally on your machine — one-off $29 unlock, no subscription: https://proxyforge.dev/#download
ProxyForge ensures that your proxies are always verified and ready to use, without the hassle of building and maintaining your own solution.
Free proxies break. ProxyForge doesn't.
The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.
Download ProxyForge free →