初始化创建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()
看我博客你代码错了,直接使用request中的sesssion就好了,你把代码用makedown形式发出来,我直接帮你稍微改下就好了我写个伪代码,记得点赞
from requests import session
session = session()
url ='xxxxxx'
data = {'xxxx'}
session.get(url=url,data=data)
#这里不需要手动设置会自动保存登入的cookies
res = session.get('你登入后后续的url')
大哥,发出来了,靠你了
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()
@失笑:
# 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: 感谢大佬
@失笑: 你自己改改吧,你的测试逻辑我感觉怪怪的0 0
@小小咸鱼YwY: 嗯,我现在都没有自己想法,纯新手一个。。
requests.Session(),python是区分大小写的
好的