Skip to content

Commit ddc98e7

Browse files
committed
新增IP代理获取工具
1 parent 99388f6 commit ddc98e7

2 files changed

Lines changed: 153 additions & 0 deletions

File tree

‎tools/proxy.json‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{"AnonymousDegree": "透明", "Area": "中国 天津市 天津市", "ScanTime": "2015-08-29 05:17:43", "Ip": "125.39.17.91", "Speed": "0.085 秒", "Type": "http", "Port": "3128"}
2+
{"AnonymousDegree": "透明", "Area": "中国 河北省 唐山市", "ScanTime": "2015-08-29 05:17:52", "Ip": "211.90.28.103", "Speed": "0.091 秒", "Type": "http", "Port": "8088"}
3+
{"AnonymousDegree": "透明", "Area": "中国 山东省 济南市", "ScanTime": "2015-08-29 05:17:35", "Ip": "119.188.115.23", "Speed": "0.097 秒", "Type": "http", "Port": "8088"}
4+
{"AnonymousDegree": "透明", "Area": "中国 河北省 唐山市", "ScanTime": "2015-08-29 05:18:14", "Ip": "211.90.28.105", "Speed": "0.129 秒", "Type": "http", "Port": "8088"}
5+
{"AnonymousDegree": "透明", "Area": "中国 天津市 天津市", "ScanTime": "2015-08-29 05:18:18", "Ip": "111.161.126.101", "Speed": "0.14 秒", "Type": "http", "Port": "80"}
6+
{"AnonymousDegree": "透明", "Area": "中国 贵州省", "ScanTime": "2015-08-29 05:18:38", "Ip": "117.135.241.93", "Speed": "0.146 秒", "Type": "http", "Port": "80"}
7+
{"AnonymousDegree": "透明", "Area": "中国 天津市 天津市", "ScanTime": "2015-08-29 05:18:18", "Ip": "111.161.126.100", "Speed": "0.148 秒", "Type": "http", "Port": "80"}
8+
{"AnonymousDegree": "透明", "Area": "中国 天津市 天津市", "ScanTime": "2015-08-29 05:18:41", "Ip": "111.161.126.99", "Speed": "0.148 秒", "Type": "qq", "Port": "80"}
9+
{"AnonymousDegree": "透明", "Area": "中国 天津市 天津市", "ScanTime": "2015-08-29 05:17:45", "Ip": "111.161.126.100", "Speed": "0.149 秒", "Type": "http", "Port": "80"}
10+
{"AnonymousDegree": "普匿", "Area": "中国 北京市 北京市", "ScanTime": "2015-08-29 05:24:00", "Ip": "54.223.207.185", "Speed": "0.152 秒", "Type": "http", "Port": "8888"}
11+
{"AnonymousDegree": "透明", "Area": "中国 山东省 泰安市", "ScanTime": "2015-08-29 05:17:44", "Ip": "60.213.189.170", "Speed": "0.157 秒", "Type": "http", "Port": "3988"}
12+
{"AnonymousDegree": "普匿", "Area": "中国 江苏省", "ScanTime": "2015-08-29 05:24:00", "Ip": "183.207.95.145", "Speed": "0.167 秒", "Type": "http", "Port": "80"}
13+
{"AnonymousDegree": "高匿", "Area": "中国 山东省 烟台市", "ScanTime": "2015-08-29 05:17:30", "Ip": "120.192.92.99", "Speed": "0.176 秒", "Type": "http", "Port": "80"}
14+
{"AnonymousDegree": "透明", "Area": "中国 河北省 唐山市", "ScanTime": "2015-08-29 05:18:21", "Ip": "211.90.28.103", "Speed": "0.185 秒", "Type": "http", "Port": "8088"}
15+
{"AnonymousDegree": "透明", "Area": "中国 河北省 唐山市", "ScanTime": "2015-08-29 05:17:43", "Ip": "211.90.28.105", "Speed": "0.189 秒", "Type": "http", "Port": "8088"}

‎tools/proxy.py‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,140 @@
11
# encoding: utf-8
22
__author__ = 'zhanghe'
3+
4+
5+
import requests
6+
from pyquery import PyQuery as Pq
7+
import json
8+
import html
9+
import random
10+
11+
source_site = 'http://ip.qiaodm.com/'
12+
13+
header = {
14+
'Host': 'ip.qiaodm.com',
15+
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36'
16+
}
17+
18+
s = requests.session()
19+
20+
ProxyList = []
21+
# IP地址 端口号 代理类型 匿名度 地域分布 响应速度 最后检测时间
22+
ProsyItem = {
23+
'Ip': '',
24+
'Port': '',
25+
'Type': '',
26+
'AnonymousDegree': '',
27+
'Area': '',
28+
'Speed': '',
29+
'ScanTime': ''
30+
}
31+
32+
# 真实端口映射关系(class 端口号)
33+
PortDict = {
34+
'GEA': '80',
35+
'GEGEA': '8080',
36+
'GEHAE': '8088',
37+
'DBZAE': '3988',
38+
'HBBAE': '8888',
39+
'CFACE': '3128',
40+
}
41+
42+
43+
def scan_proxy():
44+
"""
45+
扫描代理资源
46+
:return:
47+
"""
48+
# 抓取页面
49+
file_html = s.get(source_site).content
50+
51+
# 保存文件
52+
# with open('test.html', 'a') as f:
53+
# f.write(file_html.encode('utf-8'))
54+
#
55+
# # 读取抓取的页面
56+
# with open('test.html', 'r') as f:
57+
# file_html = f.read()
58+
59+
text_pq = Pq(file_html)
60+
tr_list = text_pq('tbody').find('tr[style="text-align: center;"]')
61+
print '单页共 %s 条记录' % len(tr_list)
62+
for tr_item in tr_list:
63+
# print Pq(tr_item).html()
64+
# print('---------------------')
65+
td_list = Pq(tr_item).find('td')
66+
# print '单条共 %s 列字段' % len(td_list)
67+
field_list = []
68+
for td_item in Pq(td_list):
69+
field = Pq(td_item).text()
70+
field_list.append(field)
71+
# print field
72+
# print('++++++++++++++++++')
73+
74+
# 特殊处理ip地址
75+
ip = Pq(td_list).eq(0).html()
76+
# 去除干扰信息
77+
ip = html.replace_html(ip, r'<p style="display:none;"/>')
78+
ip = html.replace_html(ip, r'<p style="display: none;"/>')
79+
ip = html.replace_html(ip, r'<p style=.*?display:.*?none;.*?>.*?</p>')
80+
# 去除标签
81+
ip = html.strip_html(ip)
82+
# print ip
83+
# 过滤掉非法ip地址
84+
if len(ip.split('.')) != 4:
85+
continue
86+
87+
# 特殊处理端口
88+
port_key = Pq(td_list).eq(1).attr('class').split()[1]
89+
if port_key not in PortDict:
90+
print '发现新端口: %s' % port_key
91+
continue
92+
port = PortDict.get(port_key, '')
93+
94+
ProsyItem['Ip'] = ip.replace(' ', '')
95+
ProsyItem['Port'] = port
96+
ProsyItem['Type'] = field_list[2].strip()
97+
ProsyItem['AnonymousDegree'] = field_list[3].strip()
98+
ProsyItem['Area'] = field_list[4].strip()
99+
ProsyItem['Speed'] = field_list[5].strip()
100+
ProsyItem['ScanTime'] = field_list[6].strip()
101+
# print ProsyItem
102+
proxy_item = json.dumps(ProsyItem, ensure_ascii=False)
103+
html.save_file('proxy.json', proxy_item + '\n', 'a')
104+
105+
106+
def get_list():
107+
"""
108+
获取代理列表
109+
:return:
110+
"""
111+
proxy_list = []
112+
for each_line in html.read_file_each_line('proxy.json'):
113+
item = json.loads(each_line)
114+
proxy_list.append(item)
115+
# print json.dumps(proxy_list, ensure_ascii=False, indent=4)
116+
return proxy_list
117+
118+
119+
def get_item():
120+
"""
121+
随机获取一个可用代理
122+
:return:
123+
"""
124+
proxy_list = get_list()
125+
random.shuffle(proxy_list)
126+
# print json.dumps(proxy_list[0], ensure_ascii=False, indent=4)
127+
return proxy_list[0]
128+
129+
130+
def rm_item():
131+
"""
132+
删除一个不可用代理
133+
:return:
134+
"""
135+
pass
136+
137+
if __name__ == "__main__":
138+
# scan_proxy()
139+
# get_list()
140+
get_item()

0 commit comments

Comments
 (0)