-
Notifications
You must be signed in to change notification settings - Fork 2
/
urlutil.py
145 lines (124 loc) · 6 KB
/
urlutil.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
# _*_ coding:utf-8 _*_
import requests
import re
import random
import time
class UrlUtil:
def __init__(self):
self.iplist = []
self.getIPList()
# 获取请求头列表
self.user_agent_list = [
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
"Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"
]
# 获取代理地址
def getIPList(self):
response = requests.get('http://31f.cn/area/%E4%B8%AD%E5%9B%BD/')
content = response.text
pattern = re.compile(r'<tr>.*?<td>\d.*?</td>.*?<td>(.*?)</td>.*?<td>(.*?)</td>',re.S)
items = re.findall(pattern,content)
for item in items:
self.iplist.append(item[0]+':'+item[1])
# requests post请求
def post(self, url, data, session, cookies=None, timeouts=10, header = None, proxy=None, num_retries=6):
UA = random.choice(self.user_agent_list)
mHeader = {'User-Agent': UA,'Content-Type':'application/x-www-form-urlencoded'}
if header:
mHeader = dict(mHeader,**header)
if proxy == None:
try:
if cookies:
return session.post(url,data=data, cookies=cookies, headers=mHeader, timeout=timeouts)
else:
return session.post(url,data=data, headers=mHeader, timeout=timeouts)
except Exception as e:
# print(str(e))
if num_retries > 0:
time.sleep(5)
print('获取网页出错,5S后将获取倒数第:'+ str(num_retries) +'次')
return self.post(url, data, session, cookies, timeouts, header, proxy, num_retries-1)
else:
print(u'开始使用代理')
time.sleep(5)
IP = ''.join(str(random.choice(self.iplist)).strip())
proxy = {'http': IP}
return self.post(url, data, session, cookies, timeouts, header, proxy)
else:
try:
IP = ''.join(str(random.choice(self.iplist)).strip())
proxy = {'http': IP}
if cookies:
return session.post(url,data=data, cookies=cookies,proxies=proxy, headers=mHeader, timeout=timeouts)
else:
return session.post(url,data=data, headers=mHeader,proxies=proxy,timeout=timeouts)
except:
if num_retries > 0:
time.sleep(5)
IP = ''.join(str(random.choice(self.iplist)).strip())
proxy = {'http': IP}
print('正在更换代理,5S后将重新获取倒数第'+ str(num_retries) +'次')
print('当前代理是:'+ str(proxy))
return self.post(url, data, session, cookies, timeouts, header, proxy, num_retries-1)
else:
print(u'代理也不好使了!取消代理')
return self.post(url, data, session, cookies, timeouts, header)
# requests get请求
def get(self, url, session, cookies=None, timeouts=10, header = None, proxy=None, num_retries=6):
UA = random.choice(self.user_agent_list)
mHeader = {'User-Agent': UA,'Content-Type':'application/x-www-form-urlencoded'}
if header:
mHeader = dict(mHeader,**header)
if proxy == None:
try:
if cookies:
return session.get(url, cookies=cookies, headers=mHeader, timeout=timeouts)
else:
return session.get(url, headers=mHeader, timeout=timeouts)
except Exception as e:
# print(str(e))
if num_retries > 0:
time.sleep(5)
print('获取网页出错,5S后将获取倒数第:'+ str(num_retries) +'次')
return self.get(url, session, cookies, timeouts, header, proxy, num_retries-1)
else:
print(u'开始使用代理')
time.sleep(5)
IP = ''.join(str(random.choice(self.iplist)).strip())
proxy = {'http': IP}
return self.get(url, session, cookies, timeouts, header, proxy)
else:
try:
IP = ''.join(str(random.choice(self.iplist)).strip())
proxy = {'http': IP}
if cookies:
return session.get(url, cookies=cookies,proxies=proxy, headers=mHeader, timeout=timeouts)
else:
return session.get(url, headers=mHeader,proxies=proxy,timeout=timeouts)
except:
if num_retries > 0:
time.sleep(5)
IP = ''.join(str(random.choice(self.iplist)).strip())
proxy = {'http': IP}
print('正在更换代理,5S后将重新获取倒数第'+ str(num_retries) +'次')
print('当前代理是:'+ str(proxy))
return self.get(url, session, cookies, timeouts, header, proxy, num_retries-1)
else:
print(u'代理也不好使了!取消代理')
return self.get(url, session, cookies, timeouts, header)