簡體   English   中英

如何查看請求數據?

[英]How can see the request data?

這是我的代碼,我使用 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'

我在網上找了很久。 但是沒有用。 請幫助或嘗試提供一些想法如何實現這一目標。

當 Request 與.post().get()一起發送時,它會變成requests.PreparedRequest 不幸的是,沒有可用的未編碼data了。

您可以從 POST 響應中獲得login_response.request.body ,但此時已將其編碼為表單數據。

要將它變成一個很好用的 dict 你可以使用這個:

# 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))

你應該在下面使用

print login_response.text

而不是這個?

 print login_response.request.data

基本上你是在打印響應內容而不是你發送的請求?

其他請求數據本身對您可用,因為您將它與請求一起傳遞,因此如果您願意,可以直接打印它,例如

print data

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM