-
Notifications
You must be signed in to change notification settings - Fork 61
/
utils.py
140 lines (119 loc) · 4.48 KB
/
utils.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
138
139
140
# _*_ coding:utf-8 _*_
# author: liuyunfz
import requests
from loguru import logger
from requests import post, get
import yaml
import pyDes
import binascii
import os
from time import sleep
from classis.SelfException import RequestException
from lxml.etree import _ElementUnicodeResult
with open("config.yml", "r", encoding="utf-8") as f:
config = yaml.safe_load(f)
glo_headers = config.get("GloConfig").get("headers")
glo_timeout = config.get("GloConfig").get("timeout")
logger.success("Loaded config successfully")
if_delay = config.get("GloConfig").get("delay").get("enable")
time_delay = config.get("GloConfig").get("delay").get("time")
ses = requests.session()
def doGet(url: str, headers: 'dict|str' = glo_headers, ifFullBack: bool = False) -> 'str|requests.Response':
"""
调用requests进行Get请求,并输出日志
:param url: 欲访问的链接地址
:param headers: 请求携带的headers,默认为config文件中GloConfig.headers
:param ifFullBack 是否返回完整的Response信息
:return: 返回网页文本信息,即html.text
"""
try:
logger.debug("Do Get to Url %s" % url)
ses.headers.clear()
ses.headers.update(headers)
sleep(time_delay) if if_delay else None
html = ses.get(url=url, timeout=glo_timeout)
if ifFullBack:
return html
if html.status_code == 200:
return html.text
else:
raise RequestException(html, 0)
except Exception as e:
logger.error(f"Get Url {url} Error\n {e}")
def doPost(url: str, headers: 'dict|str' = glo_headers, data: 'dict|str' = "", ifFullBack: bool = False) -> 'str|requests.Response':
"""
调用requests进行Post请求,并输出日志
:param url: 欲访问的链接地址
:param headers: 请求携带的headers,默认为config文件中GloConfig.headers
:param data: 请求携带的data数据,默认为空
:param ifFullBack 是否返回完整的Response信息
:return: 返回网页文本信息,即html.text
"""
try:
logger.debug("Do Post to Url %s" % url)
logger.debug("With data: %s" % data)
ses.headers.clear()
ses.headers.update(headers)
sleep(time_delay) if if_delay else None
html = ses.post(url=url, data=data, timeout=glo_timeout)
if ifFullBack:
return html
if html.status_code == 200:
return html.text
else:
raise RequestException(html, 1)
except Exception as e:
logger.error(f"Post Url {url} Error\n {e}")
def xpath_first(element, path):
"""
返回xpath获取到的第一个元素,如果没有则返回空字符串
由于 lxml.etree._Element._Element 为私有类,所以不予设置返回值类型
:param element: etree.HTML实例
:param path: xpath路径
:return: 第一个元素或空字符串
"""
if type(element) in (str, int):
return element
res = element.xpath(path)
if type(res) == _ElementUnicodeResult:
return res
if len(res) == 1:
return res[0]
else:
return ""
def direct_url(old_url: str, headers: dict, ifLoop: bool = False) -> str:
"""
返回重定向后的真实Url
:param old_url: 待重定向的链接地址
:param headers: 附带的请求头
:param ifLoop: 是否迭代查询直至无跳转
:return: 最终的Url
"""
logger.debug("Redirect old url: %s" % old_url)
location_new = None
try:
while location_new is None:
rsp = requests.get(url=old_url, headers=headers, allow_redirects=False)
location_new = rsp.headers.get("Location")
if ifLoop:
return location_new | old_url
if location_new is None:
logger.debug("Final url: %s" % old_url)
return old_url
else:
old_url = location_new
location_new = None
except Exception as e:
logger.error(e)
return ""
def encrypt_des(msg, key):
des_obj = pyDes.des(key, key, pad=None, padmode=pyDes.PAD_PKCS5)
secret_bytes = des_obj.encrypt(msg, padmode=pyDes.PAD_PKCS5)
return binascii.b2a_hex(secret_bytes)
def clear_console():
if os.name == "nt":
os.system("cls")
else:
os.system("clear")
if __name__ == '__main__':
doGet("https://www.6yfz.cn/")