Python requests post 请求获取响应数据错误问题

近日,使用 Python requests 请求, post 发送 Query String Parameters 数据时发现获取响应数据错误问题。

Python requests post 请求获取响应数据错误问题

源代码:

  1. import requests  
  2.   
  3. headers = {  
  4.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'  
  5. }  
  6. index_url = 'https://r.inews.qq.com/web_feed/getWebList'  
  7. params = {  
  8.             "adcode": "110000",  
  9.               "device_id": "1_12345678" ,  
  10.               "channel_id": "news_news_games",  
  11.               "uin": "test",  
  12.               "qimei36": "o12345678",  
  13.               "base_req": {"from": "qq_web"},  
  14.               "forward": "20",  
  15.               "is_local_chlid": ""  
  16.               }  
  17. response = requests.post(index_url, headers=headers, params=params).json()  

运行以上代码发现,返回响应数据错误,后经一番搜索,发现 params 内容为 json 格式时,需把 params= 换为 json= 即可返回正常数据。

附正确代码:

  1. import requests  
  2.   
  3. headers = {  
  4.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'  
  5. }  
  6. index_url = 'https://r.inews.qq.com/web_feed/getWebList'  
  7. params = {  
  8.             "adcode": "110000",  
  9.               "device_id": "1_12345678" ,  
  10.               "channel_id": "news_news_games",  
  11.               "uin": "test",  
  12.               "qimei36": "o12345678",  
  13.               "base_req": {"from": "qq_web"},  
  14.               "forward": "20",  
  15.               "is_local_chlid": ""  
  16.               }  
  17. response = requests.post(index_url, headers=headers, json=params).json()  
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
搜索