簡體   English   中英

urllib2中請求“數據”和“參數”的對應參數是什么?

[英]What are the corresponding parameters of Requests “data” and “params” in urllib2?

我已經成功實現了python Requests模塊,將POST請求發送到指定的服務器

resp = request.request(“ POST”,URL,代理,數據,標頭,參數,超時)

但是,由於某種原因,我現在需要使用python urllib2模塊進行查詢。 對於urllib2.urlopen的參數“數據”,據我了解,它有助於形成查詢字符串(與請求“參數”相同)。 另一方面,requests.request的參數“ data”用於填充請求正文。

在搜索並閱讀了許多帖子,示例和文檔之后, 我仍然無法弄清urllib2中request.request的“ data”的對應參數是什么

任何建議深表感謝! 謝謝。

-Janton

所謂的無所謂-是在正確的位置傳遞它的問題。 例如,在此示例中,POST數據是字典(名稱可以是任何東西)。 字典是urlencoded,而urlencoded名稱又可以是任何東西,但我選擇了“ postdata”,這是發布的數據

import urllib   # for the urlencode
import urllib2

searchdict = {'q' : 'urllib2'}
url = 'https://duckduckgo.com/html'
postdata = urllib.urlencode(searchdict)

req = urllib2.Request(url, postdata)
response = urllib2.urlopen(req)

print response.read()
print response.getcode()

如果您的POST數據是純文本(不是Python類型,例如字典),則無需urllib.urlencode就可以使用:

import urllib2

searchstring = 'q=urllib2'
url = 'https://duckduckgo.com/html'

req = urllib2.Request(url, searchstring)
response = urllib2.urlopen(req)

print response.read()
print response.getcode()

暫無
暫無

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

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