forked from qi20172017/weibo_spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
99 lines (82 loc) · 2.27 KB
/
tools.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
import time
import os
import requests
import socket
import random
class TxtHandler:
@staticmethod
def read_lines(path):
with open(path, 'r', encoding='utf-8')as f:
res = f.readlines()
return res
@staticmethod
def write_lines(path, line):
line = line + '\n'
with open(path, 'a', encoding='utf-8')as f:
f.write(line)
class PathHandler:
@staticmethod
def dir_path():
"""
Get current directory path
:return: path
"""
return os.path.dirname(os.path.abspath(__file__))
@staticmethod
def join_path(filename):
"""
Get current directory file path
:param filename: target file
:return: path
"""
return os.path.join(PathHandler.dir_path(), filename)
class IpHandler:
def get_proxy(self):
ip_url = 'http://api.ip.data5u.com/dynamic/get.html?order=5b14a7c835defdba1877c544bebb7c28&sep=4'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36', }
while True:
response = requests.get(url=ip_url, headers=headers).text
if 'too many requests' in response:
time.sleep(0.5)
continue
else:
prox = {'http': 'http://' + response.strip(' '),
}
break
return prox
def get_host_ip():
"""
查询本机ip地址
:return:
"""
try:
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.connect(('8.8.8.8',80))
ip=s.getsockname()[0]
finally:
s.close()
return ip
def sleep_time(level=1):
"""
睡眠级别
1:睡眠3~5秒
2:8~12秒
3:30~40秒
:param level:
:return: None
"""
if level == 1:
time.sleep(random.uniform(3, 5))
elif level == 2:
time.sleep(random.uniform(8, 12))
elif level == 3:
time.sleep(random.uniform(30, 40))
elif level == 9:
time.sleep(60*60*random.uniform(1, 2))
if __name__ == '__main__':
# print(PathHandler.dir_path())
# print(PathHandler.join_path('username_fir.txt'))
# print(IpHandler.get_proxy())
ip = get_host_ip()
print(ip)