-
Notifications
You must be signed in to change notification settings - Fork 135
/
proxyScraper.py
168 lines (130 loc) · 4.79 KB
/
proxyScraper.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import argparse
import re
import threading
import requests
from bs4 import BeautifulSoup
class Scraper:
def __init__(self, method, _url):
self.method = method
self._url = _url
def get_url(self, **kwargs):
return self._url.format(**kwargs, method=self.method)
def get_response(self):
return requests.get(self.get_url())
def handle(self, response):
return response.text
def scrape(self):
response = self.get_response()
proxies = self.handle(response)
pattern = re.compile(r"\d{1,3}(?:\.\d{1,3}){3}(?::\d{1,5})?")
return re.findall(pattern, proxies)
# From spys.me
class SpysMeScraper(Scraper):
def __init__(self, method):
super().__init__(method, "https://spys.me/{mode}.txt")
def get_url(self, **kwargs):
mode = "proxy" if self.method == "http" else "socks" if self.method == "socks" else "unknown"
if mode == "unknown":
raise NotImplementedError
return super().get_url(mode=mode, **kwargs)
# From proxyscrape.com
class ProxyScrapeScraper(Scraper):
def __init__(self, method, timeout=1000, country="All"):
self.timout = timeout
self.country = country
super().__init__(method,
"https://api.proxyscrape.com/?request=getproxies"
"&proxytype={method}"
"&timeout={timout}"
"&country={country}")
def get_url(self, **kwargs):
return super().get_url(timout=self.timout, country=self.country, **kwargs)
# From proxy-list.download
class ProxyListDownloadScraper(Scraper):
def __init__(self, method, anon):
self.anon = anon
super().__init__(method, "https://www.proxy-list.download/api/v1/get?type={method}&anon={anon}")
def get_url(self, **kwargs):
return super().get_url(anon=self.anon, **kwargs)
def get_response(self):
return requests.session().get(self.get_url())
# For websites using table in html
class GeneralTableScraper(Scraper):
def handle(self, response):
soup = BeautifulSoup(response.text, "html.parser")
proxies = set()
table = soup.find("table", attrs={"class": "table table-striped table-bordered"})
for row in table.findAll("tr"):
count = 0
proxy = ""
for cell in row.findAll("td"):
if count == 1:
proxy += ":" + cell.text.replace(" ", "")
proxies.add(proxy)
break
proxy += cell.text.replace(" ", "")
count += 1
return "\n".join(proxies)
scrapers = [
SpysMeScraper("http"),
SpysMeScraper("socks"),
ProxyScrapeScraper("http"),
ProxyScrapeScraper("socks4"),
ProxyScrapeScraper("socks5"),
ProxyListDownloadScraper("https", "elite"),
ProxyListDownloadScraper("http", "elite"),
ProxyListDownloadScraper("http", "transparent"),
ProxyListDownloadScraper("http", "anonymous"),
GeneralTableScraper("https", "http://sslproxies.org"),
GeneralTableScraper("http", "http://free-proxy-list.net"),
GeneralTableScraper("http", "http://us-proxy.org"),
GeneralTableScraper("socks", "http://socks-proxy.net"),
]
def verbose_print(verbose, message):
if verbose:
print(message)
def scrape(method, output, verbose):
methods = [method]
if method == "socks":
methods += ["socks4", "socks5"]
proxy_scrapers = [s for s in scrapers if s.method in methods]
if not proxy_scrapers:
raise ValueError("Method not supported")
verbose_print(verbose, "Scraping proxies...")
proxies = []
def scrape_scraper(scraper):
verbose_print(verbose, f"Looking {scraper.get_url()}...")
proxies.extend(scraper.scrape())
threads = []
for scraper in proxy_scrapers:
threads.append(threading.Thread(target=scrape_scraper, args=(scraper,)))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
verbose_print(verbose, f"Writing {len(proxies)} proxies to file...")
with open(output, "w") as f:
f.write("\n".join(proxies))
verbose_print(verbose, "Done!")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-p",
"--proxy",
help="Supported proxy type: " + ", ".join(sorted(set([s.method for s in scrapers]))),
required=True,
)
parser.add_argument(
"-o",
"--output",
help="Output file name to save .txt file",
default="output.txt",
)
parser.add_argument(
"-v",
"--verbose",
help="Increase output verbosity",
action="store_true",
)
args = parser.parse_args()
scrape(method=args.proxy, output=args.output, verbose=args.verbose)