如何在單個請求中獲取http響應的內容以及響應url(不是請求的url)。獲取http請求的內容以及單個請求中的響應url
爲了得到響應我用:
from urllib2 import Request,urlopen
try:
headers = { 'User-Agent' : 'Mozilla/5.0 (X11; U; Linux i686; en-US;)' }
request = Request(url, data, headers)
print urlopen(request).read()
except Exception, e:
raise Exception(e)
如果我想只有頭(頭會有響應的網址),我用
try:
headers = { 'User-Agent' : 'Mozilla/5.0 (X11; U; Linux i686; en-US;)' }
request = Request(url, data, headers)
request.get_method = lambda : 'HEAD'
print urlopen(request).geturl()
except Exception, e:
raise Exception(e)
我做兩個請求來獲取內容&網址。 我怎樣才能在一個請求中獲得兩個。如果我的函數返回內容&作爲元組更好。
我只需要響應url,因爲我請求的url可能會重定向到其他頁面。 – Garfield 2013-02-08 18:13:43