-
Notifications
You must be signed in to change notification settings - Fork 23
/
fetcher.py
63 lines (49 loc) · 1.42 KB
/
fetcher.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
# coding: utf8
"""代理采集器
"""
import re
import socket
import urllib2
from config import (
PROXY_SITES, PROXY_DEST, UA,
PROXY_REGX, FETCH_TIMEOUT
)
class Fetcher(object):
"""代理采集器
"""
def __init__(self, proxy_dest=PROXY_DEST):
self.proxy_dest = proxy_dest
self.fetch_sites = PROXY_SITES
self.timeout = FETCH_TIMEOUT
def fetch(self):
"""采集代理
"""
for site in self.fetch_sites:
request = urllib2.Request(site)
request.add_header("User-Agent", UA)
try:
print "fetching %s" % site
response = urllib2.urlopen(
request, timeout=self.timeout).read()
proxies = re.findall(PROXY_REGX, response)
except (urllib2.URLError, socket.error) as exc:
print "[ERROR] fetch %s failed: %s" % (site, str(exc))
proxies = []
else:
print "[OK] %s proxies from %s" % (len(proxies), site)
self.output(proxies)
print "done!"
def output(self, proxies):
"""输出
@proxies, list, 代理列表
"""
if not proxies:
return
with open(self.proxy_dest, "a") as fpt:
for proxy in proxies:
fpt.write("%s\n" % proxy)
def main():
""" main """
Fetcher().fetch()
if __name__ == "__main__":
main()