Created
January 27, 2021 06:25
-
-
Save aditya-gupta-sde/4df78fd82866cf529ad0fdda96096007 to your computer and use it in GitHub Desktop.
Get a list of proxy IP Addresses (useful for IP Rotation during scraping)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from lxml.html import fromstring | |
import random | |
import requests | |
def get_proxies(): | |
""" | |
Gather a list of some active proxies from https://free-proxy-list.net/ | |
:return: List of IP Addresses | |
""" | |
url = 'https://free-proxy-list.net/' | |
response = requests.get(url) | |
parser = fromstring(response.text) | |
proxies = set() | |
for i in parser.xpath('//tbody/tr')[:1000]: | |
if i.xpath('.//td[7][contains(text(),"yes")]'): | |
# Grabbing IP and corresponding PORT | |
proxy = ":".join([i.xpath('.//td[1]/text()')[0], i.xpath('.//td[2]/text()')[0]]) | |
proxies.add(proxy) | |
return list(proxies) | |
proxy_ips = get_proxies() | |
# To randomly select an IP from the collected proxies | |
random_ip = random.choice(proxy_ips) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment