-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxies.py
57 lines (48 loc) · 1.84 KB
/
proxies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from random import choice
import requests
from lxml.html import fromstring
# Credit for proxies: https://www.alexbilz.com/post/rotating-free-elite-proxies-in-python/
class Proxies:
def __init__(self):
self.proxy = None
self.proxies = []
@staticmethod
def get_proxies():
url = 'https://sslproxies.org/'
response = requests.get(url)
parser = fromstring(response.text)
p = []
for i in parser.xpath('//tbody/tr'):
if i.xpath('.//td[7][contains(text(),"yes")]'):
if i.xpath('.//td[5][contains(text(),"elite proxy")]'):
proxy = ":".join([i.xpath('.//td[1]/text()')[0], i.xpath('.//td[2]/text()')[0]])
p.append(proxy)
return p
@staticmethod
def to_proxy(proxy):
return {"http": proxy, "https": proxy}
def get(self):
if len(self.proxies) < 2:
self.proxies = self.get_proxies()
self.proxy = choice(self.proxies)
return self.to_proxy(self.proxy)
def remove(self):
# Remove invalid proxy from pool
self.proxies.remove(self.proxy)
self.proxy = self.get()
def scrape(self, url, **kwargs):
# Retry until request was sucessful
while True:
try:
proxy = self.get()
print("Proxy currently being used: {}".format(proxy))
response = requests.get(url, proxies=proxy, timeout=5, **kwargs)
break
# if the request is successful, no exception is raised
except requests.exceptions.ProxyError:
print("Proxy error, choosing a new proxy")
self.remove()
except requests.exceptions.ConnectTimeout:
print("Connect error, choosing a new proxy")
self.remove()
return response