import json
import os
import requests
from requests.cookies import RequestsCookieJar
def save_cookie():
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
}
url = "http://www.test.com"
session = requests.session()
cookieJar = getcookies_decode_to_dict()
response = session.get(url, headers=headers, cookies=cookieJar)
cookies = session.cookies.get_dict()
path = os.getcwd() + '/cookies/'
with open(path + 'cookies.txt', 'a+') as f:
f.write(json.dumps(cookies))
f.close()
print(response.content.decode('utf-8'))
def getcookies_decode_to_dict():
path = os.getcwd() + '/cookies/'
if not os.path.exists(path):
print('Cookie文件不存在,请先运行cookiesload.py')
else:
cookies_dict = {}
with open(path + 'cookies.txt', 'r') as f:
js = f.read()
if not js:
return
cookies = json.loads(js)
return cookies
for cookie in cookies:
cookies_dict[cookie['name']] = cookie['value']
return cookies_dict
def getcookies_decode_to_cookiejar():
path = os.getcwd() + '/cookies/'
if not os.path.exists(path):
print('Cookie文件不存在,请先运行cookiesload.py')
else:
cookiejar = RequestsCookieJar()
with open(path + 'cookies.txt', 'r') as f:
cookies = json.loads(str(f.read()))
for cookie in cookies:
cookiejar.set(cookie['name'], cookie['value'])
return cookiejar
save_cookie()
将cookies保存到文件(python版)
?著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事?!?“怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- numpy h5py scipy 读取mat文件并存为npy格式文件 具体见代码,注意h5py的转置问题 npy文...