簡體   English   中英

http請求:從urllib2到python 2.7請求

[英]http requests : from urllib2 to requests python 2.7

我一直在嘗試將腳本中的所有http請求從使用urllib1 / 2切換到使用請求執行更多高級請求。

顯然,我沒有這樣做。

誰能解釋一下以下兩者之間的區別:

import urllib,urllib2
data=urllib.urlencode(params)
req=urllib2.Request("http://play.pokemonshowdown.com/action.php",data,{ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36)' })
response=urllib2.urlopen(req)
response.read()

和:

import requests
r=requests.post("http://play.pokemonshowdown.com/action.php",params=params,headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36)' })
r.text

第一個產生東西:

']{"curuser":{"userid":"artemisfowl2","usernum":"5717357","username":"Artemis Fowl 2","email":"","registertime":"1444568721","group":"1","banstate":"0","ip":"78.226.235.170","loggedin":true,"outdatedpassword":false},"actionsuccess":true,"assertion":"3f457310c72114c316010110c6dc8f319593d2952b0ebf9a5621b0cdead6db9fce3b7324f158668f82b137135e756b01886df1e297854e536a32166706e93b577ff3a51ee20e19de94fcd7cec4a8668da750d144bbb2cb3fa58fe06fd3d72409f38ed92a28e3631bf0cce73733d0d66b73a147a86e1d7db900c3c46b7de2b429,artemisfowl2,2,1444579736,;2c5deb8c47691071bcd624078e9c7ca1bfa42d7b0ca92109315b7049342206718db2b198fd16f9983049209c23a303ec4dc8bb7a101301ec4603ff19719e4022a3bb7266203c371620407051c94512d038ff6128919c65313d2c006b38f2f0e10ea14870aa2f3132f80904b49a1bed049e0d45000088c95d9ea0b7e34fdd1fa2da4f6f8fb18afa39243c628c8130633a2639ce62ff6b01bd1c562cc009361e2b3edf1a41d96043c32d5cb86c7082cab45c1ced2fd438a12cf8eeb6d0638a4c7f0b9793152bb709f21b79311cfc74c80f1878852ae1b3658197495de98e07b0e3bb9ea949cbc0501fab39cbb13ff7db22077198a61397072be47dacbc7929e8dde5069c2e307dacb57ce41cdc832866aa79c625bfba320699a838bf33d31571e9acb29d846938a232870ae7d42b7689f6b8b03b4318dada3d4aba22e5f46eed0d4a3137bbbcde4c7ebadcb8c42dc5b495f4a5e8e0f4cd18da6013d5d119b5050d2bae9a1281792b83d350b96cd34ebf892116b9bc43d654e30438223315214310c689630b40185f3dc36a557e2cafda3d9272a525f021b56585ca189036649ce8286e13aadd60c076f7efc9003b2819f27e8496130aad405448424fd49720b8faa2661fc139b25ed770edf5eff70fd939eb42d31495edf61b3a0df1328ca95129f7becc5d62b27e70594f45337f633c5e1901499ab822cfb2cda9fa87ee213ca6"}'

雖然第二個沒有:

u''

我可以確認參數在兩種情況下均正確編碼,並且網址相同。 另外,我要執行的請求是POST請求。

那么,為什么我得到一個空答案?

我認為您的端點期望數據位於請求正文中,而不是查詢參數中,請嘗試使用

import requests
r=requests.post("http://play.pokemonshowdown.com/action.php",data=params,headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36)' })
r.text

我認為應該可以解決您的問題。

要回答您的問題:

urllib2.Request("http://play.pokemonshowdown.com/action.php",data,{ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36)' })

通過提供data參數將數據添加到請求的正文中,您將隱式創建一個post消息,並在請求的正文中添加數據。

該行:

r=requests.post("http://play.pokemonshowdown.com/action.php",params=params,headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36)' })

將數據添加到url(例如: www.hostname.com?param1=this&param2=that&param3=theother this& www.hostname.com?param1=this&param2=that&param3=theother )而不是請求的正文中。 通過將params更改為data ,應按照服務器的期望將數據添加到主體。

暫無
暫無

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

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