-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsearch_proxy.py
More file actions
70 lines (56 loc) · 1.76 KB
/
search_proxy.py
File metadata and controls
70 lines (56 loc) · 1.76 KB
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
from time import sleep
import cssutils
import requests
from bs4 import BeautifulSoup
import datetime
from pytz import *
from model import Proxy, connect_mongodb
def search_proxy():
url = "http://cn-proxy.com/"
data = ""
# while not data or data == "":
# try:
# data = requests.get(url).text
# except Exception:
# sleep(2)
# continue
with open('/home/chenxiao/document/data', 'rt') as f:
data = f.read()
soup = BeautifulSoup(data, 'html.parser')
tbody = soup.findAll('tbody')[1]
tr_list = tbody.findAll('tr')
for tr in tr_list:
td_list = tr.findAll('td')
proxy = Proxy()
speed = get_speed(td_list[3])
if speed < 70:
continue
proxy.speed = speed
proxy.url = td_list[0].text + ":" + td_list[1].text
proxy.position = td_list[2].text
time_string = td_list[4].text
time = datetime.datetime.strptime(time_string, '%Y-%m-%d %H:%M:%S')
time = timezone('Asia/Shanghai').localize(time)
utc_time = time.astimezone(utc)
proxy.last_check = utc_time
if not Proxy.objects(url=proxy.url):
print("加入代理服务器: {}".format(proxy.url))
proxy.save()
def get_speed(td):
speed_style = td.find('strong').attrs['style']
speed_style_width = cssutils.parseStyle(speed_style).width
speed = int(speed_style_width[:-1])
return speed
# connect_mongodb()
# search_proxy()
#
# # 如果需要使用代理,你可以通过为任意请求方法提供 proxies 参数来配置单个请求:
#
# import requests
#
# proxies = {
# "http": "http://10.10.1.10:3128",
# "https": "http://10.10.1.10:1080",
# }
#
# requests.get("http://example.org", proxies=proxies)