http 请求:从 urllib2 到请求 python 2.7

http requests : from urllib2 to requests python 2.7

我一直在尝试将脚本中的所有 http 请求从使用 urllib1/2 切换为使用 Requests 来执行更高级的请求。

显然,我没有这样做。

谁能给我解释一下两者的区别:

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''

我可以确认参数在两种情况下都已正确编码,并且 url 相同。另外,我想做的请求是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)' })

通过提供数据参数将数据添加到请求正文中,您隐式创建了一个 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)而不是请求的主体。通过将 params 更改为 data,数据应该按照服务器的预期添加到正文中。