这是我的代码,我使用requests.python2.7
#create requests
requests_vivo = requests.Session()
#login url
login_url = 'https://id.vivo.com.cn/api/login'
#captcha_url
captcha_url = 'https://id.vivo.com.cn/api/kaptcha.jpg?t=%.0f' % time.time()
#header
header = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip,deflate,sdch",
"Accept-Language": "zh-CN,zh;q=0.8",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "id.vivo.com.cn",
"Origin": "https://id.vivo.com.cn",
"Referer": "https://id.vivo.com.cn/?_%.0f"%time.time(),
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
}
#request captcha
captcha_response = requests_vivo.get(url=captcha_url,headers=header)
#write jpg
with open('captcha_pic.jpg','wb') as f:
f.write(captcha_response.content)
captcha_code = raw_input('Please input code:')
#data
data = {
"name": setting.username,
"password": encryptPasswd(setting.password),
"verificationCode": captcha_code,
"remember": "0"
}
#login request
login_response = requests_vivo.post(url=login_url,headers=header,data=data)
print login_response.request.data
这是错误,我看不到数据:
#captcha
Please input code:8men
8men
#error info
Traceback (most recent call last):
File "/home/freedom/work/app/sem/vivo/test.py", line 39, in <module>
print login_response.request.data
AttributeError: 'PreparedRequest' object has no attribute 'data'
我在网上找了很久。但是没有用。请帮助或尝试提供一些想法如何实现这一目标。
最佳答案
请求变成了 requests.PreparedRequest当使用 .post()
或 .get()
发送时。不幸的是,未编码数据
不再可用。
您可以从 POST 响应中获得的是 login_response.request.body
,但此时它已被编码为表单数据。
要将它变回一个很好用的字典,你可以使用这个:
# py2
import urlparse
dict(urlparse.parse_qsl(login_response.request.body))
或
# py3
from urllib.parse import parse_qsl
dict(parse_qsl(login_response.request.body))
关于python - 怎么才能看到请求数据呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48018622/