forked from Lasx/jd_mask
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
71 lines (62 loc) · 2.42 KB
/
Copy pathutil.py
File metadata and controls
71 lines (62 loc) · 2.42 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
70
71
import json
import random
import requests
from config import global_config
from lxml import etree
def parse_json(s):
try:
begin = s.find('{')
end = s.rfind('}') + 1
return json.loads(s[begin:end])
except Exception as e:
return False
def get_cookies():
"""解析cookies内容并添加到cookiesJar"""
manual_cookies = {}
cookie_string = global_config.getRaw('config', 'cookies_String')
if len(cookie_string) == 0:
print('请设置cookie')
exit(1)
for item in cookie_string.split(';'):
key_pare = item.strip().split('=', 1)
if len(key_pare) != 2:
print('cookie格式错误')
exit(1)
name, value = key_pare
# 用=号分割,分割1次
manual_cookies[name] = value
# 为字典cookies添加内容
cookiesJar = requests.utils.cookiejar_from_dict(manual_cookies, cookiejar=None, overwrite=True)
return cookiesJar
def get_session():
# 初始化session
session = requests.session()
session.headers = {"User-Agent": global_config.getRaw('config', 'DEFAULT_USER_AGENT'),
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Connection": "keep-alive"}
checksession = requests.session()
checksession.headers = {"User-Agent": global_config.getRaw('config', 'DEFAULT_USER_AGENT'),
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Connection": "keep-alive"}
# 获取cookies保存到session
session.cookies = get_cookies()
return session
def get_sku_title():
"""获取商品名称"""
url = 'https://item.jd.com/{}.html'.format(global_config.getRaw('config', 'sku_id'))
session = get_session()
resp = session.get(url).content
x_data = etree.HTML(resp)
sku_title = x_data.xpath('/html/head/title/text()')
return sku_title[0]
def send_wechat(message):
"""推送信息到微信"""
url = 'http://sc.ftqq.com/{}.send'.format(global_config.getRaw('messenger', 'sckey'))
payload = {
"text": '抢购结果',
"desp": message
}
headers = {
'User-Agent': global_config.getRaw('config', 'DEFAULT_USER_AGENT')
}
requests.get(url, params=payload, headers=headers)