在做一个顶帖机 然后现在无法导出cookies
本帖最后由 一个账号 于 2020-3-7 10:02 编辑谢谢大神帮忙看看
txt有文件生成但是文本里没东西
import requests
import http.cookiejar as cookielib
skykiwiSession = requests.session()
skykiwiSession.cookies = cookielib.LWPCookieJar(filename='sk.txt')#这里就是出问题的地方 最后txt文件里是空的
userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0'
header = {'Origin':'https://passport.skykiwi.com',
'Referer':'https://passport.skykiwi.com/v1/login/bbslogon.do',
'User-Agent': userAgent}
def skylogin(account,password):
print('Begin Login')
postUrl='https://passport.skykiwi.com/v1/login/bbslogon.do'
postData={'username':account,'password':password,'verifycode':'1111','backurl':'http://bbs.skykiwi.com/forum.php','isRemember':'0'}
responseRes = skykiwiSession.post(postUrl,data =postData,headers=header)
print(f'statusCode= {responseRes.status_code}')
print(f'test ={responseRes.text}')
skykiwiSession.cookies.save()
print('Cookie saved')
if __name__=='__main__':
# Username = input('please enter user name')
#Password = input('please enter password')
skylogin('syliva_2011', '*****')
{:5_100:}没人可以帮吗 我也等着呢 本帖最后由 suchocolate 于 2020-6-23 19:20 编辑
requests的cookie类是RequestsCookieJar类,它不支持save方法。
但它可以转成字典,你可以遍历字典保存成文本,也可以直接保存成pickle。import requests
r = requests.get('https://www.baidu.com')
print(r.cookies.items())
with open('cookie.txt', 'w') as f:
for k, v in r.cookies.items():
print(k, '=', v)
f.write(k + '=' + v)
读取时就是反过来构建RequestsCookieJar:import requests
from requests.cookies import RequestsCookieJar
jar = RequestsCookieJar()
with open('cookie.txt','r') as f:
for item in f.readlines():
k, v = item.split('#')
jar.set(k, v)
r = requests.get('https://www.baidu.com', cookies=jar)
print(r.status_code)
页:
[1]