首页新闻找找看学习计划

python+request初始化创建cookies对象报错

0
悬赏园豆:5 [已解决问题] 解决于 2019-12-02 17:02

初始化创建cookies对象时报错。系统不知道filename哪来的。

问题补充:
 1 #coding=utf-8
 2 import unittest
 3 import requests,json
 4 
 5 try:
 6     import cookielib
 7     print("user cookielib in python2")
 8 except:
 9     import http.cookiejar as cookielib
10     print("user cookielib in python3")
11 
12 
13 useragent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
14 header = {
15     # Origin : "xxxx",
16     "Referer": "xxxx",
17     "User-Agent" : useragent,
18 }
19 
20 def bestulogin(user,pwd):
21     #xx模仿 登录
22     print("开始模拟xx登录")
23 
24     posturl = "xxxx"
25     postdata = {'loginName': user,'password' : pwd}
26     respones = requests.post(posturl, data=postdata, headers=header)
27     #无论是否登录成功,状态码一般都为200
28     print(respones.status_code)
29     print(respones.text)
30     #bestusession.cookies.save()
31 
32 def getViaResult():
33     #获取VIA测评结果
34     print('获取VIA测评结果')
35     posturl2 = "xxxxxxxxxx"
36     postdata = {'id' : 'via85', 'lang' : 'zh'}
37     respones = requests.get(posturl2,data=postdata)
38     print(respones.text)
39 
40 
41 if __name__ == "__main__":
42     #从返回值查看,有登录成功
43     bestulogin("wangye","123456")
44     getViaResult()
失笑的主页 失笑 | 初学一级 | 园豆:198
提问于:2019-11-28 15:37
< >
分享
最佳答案
0

看我博客你代码错了,直接使用request中的sesssion就好了,你把代码用makedown形式发出来,我直接帮你稍微改下就好了我写个伪代码,记得点赞

from requests import session
session = session()
url ='xxxxxx'
data = {'xxxx'}
session.get(url=url,data=data)
#这里不需要手动设置会自动保存登入的cookies



res = session.get('你登入后后续的url')

收获园豆:5
小小咸鱼YwY | 小虾三级 |园豆:1059 | 2019-11-28 17:12

大哥,发出来了,靠你了

 1 #coding=utf-8
 2 import unittest
 3 import requests,json
 4 
 5 try:
 6     import cookielib
 7     print("user cookielib in python2")
 8 except:
 9     import http.cookiejar as cookielib
10     print("user cookielib in python3")
11 
12 
13 useragent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
14 header = {
15     # Origin : "xxxx",
16     "Referer": "xxxx",
17     "User-Agent" : useragent,
18 }
19 
20 def bestulogin(user,pwd):
21     #xx模仿 登录
22     print("开始模拟xx登录")
23 
24     posturl = "xxxx"
25     postdata = {'loginName': user,'password' : pwd}
26     respones = requests.post(posturl, data=postdata, headers=header)
27     #无论是否登录成功,状态码一般都为200
28     print(respones.status_code)
29     print(respones.text)
30     #bestusession.cookies.save()
31 
32 def getViaResult():
33     #获取VIA测评结果
34     print('获取VIA测评结果')
35     posturl2 = "xxxxxxxxxx"
36     postdata = {'id' : 'via85', 'lang' : 'zh'}
37     respones = requests.get(posturl2,data=postdata)
38     print(respones.text)
39 
40 
41 if __name__ == "__main__":
42     #从返回值查看,有登录成功
43     bestulogin("wangye","123456")
44     getViaResult()

 

失笑 | 园豆:198 (初学一级) | 2019-12-02 16:47

@失笑:


# coding=utf-8
import requests

header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
}


def bestulogin(user, pwd,session):
    # xx模仿 登录
    print("开始模拟xx登录")

    posturl = "xxxx"
    postdata = {'loginName': user, 'password': pwd}
    respones = session.post(posturl, data=postdata, headers=header)
    # 无论是否登录成功,状态码一般都为200
    print(respones.status_code)
    print(respones.text)



def getViaResult(session):
    # 获取VIA测评结果
    print('获取VIA测评结果')
    posturl2 = "xxxxxxxxxx"
    postdata = {'id': 'via85', 'lang': 'zh'}
    respones = session.get(posturl2, data=postdata)
    print(respones.text)


def run(user, pwd):
    session = requests.session()
    bestulogin(user, pwd,session)
    getViaResult(session)


if __name__ == "__main__":
    # 从返回值查看,有登录成功
    run("wangye", "123456")

小小咸鱼YwY | 园豆:1059 (小虾三级) | 2019-12-02 16:56

@小小咸鱼YwY: 感谢大佬

失笑 | 园豆:198 (初学一级) | 2019-12-02 17:01

@失笑: 你自己改改吧,你的测试逻辑我感觉怪怪的0 0

小小咸鱼YwY | 园豆:1059 (小虾三级) | 2019-12-02 17:02

@小小咸鱼YwY: 嗯,我现在都没有自己想法,纯新手一个。。

失笑 | 园豆:198 (初学一级) | 2019-12-03 10:06
其他回答(1)
0

requests.Session(),python是区分大小写的

易yl龙 | 园豆:198 (初学一级) | 2019-11-29 18:57

好的

支持(0) 反对(0) 失笑 | 园豆:198 (初学一级) | 2019-12-02 16:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册