Python urllib2 發送HTTP Request

urllib2 是Python自帶的標準模塊, 用來發送HTTP Request的。  類似於 .NET中的,  HttpWebRequest類

 

urllib2 的優點

Python urllib2 發出的HTTP Request, 能自動被Fiddler截獲, 方便了調試。

Python 可以自動處理Cookie

 

urllib2 的缺點

Python urllib2 發出的http Request, 中的header 會被修改成“首字母大寫”,

比如你的代碼裏寫的header 是: content-TYPE=application/x-www-form-urlencoded ,  會被修改爲 Content-Type=application/x-www-form-urlencoded

實例一,  Get方法, 並且自定義header

# -* - coding: UTF-8 -* -  
import urllib2

request = urllib2.Request("http://www.baidu.com/")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
print response.getcode()
print response.geturl()
print response.read()

實例二, post方法

# -* - coding: UTF-8 -* -  
import urllib2
import urllib

request = urllib2.Request("http://passport.cnblogs.com/login.aspx")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
data={"tbUserName":"test_username", "tbPassword":"test_password"}

response = urllib2.urlopen(request, urllib.urlencode(data))
print response.getcode()
print response.geturl()
print response.read()

實例三: Cookie 的處理

# -* - coding: UTF-8 -* -  
import urllib2
import urllib
import cookielib

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

request = urllib2.Request("https://dynamic.12306.cn/otsweb/")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
data={"tbUserName":"test_username", "tbPassword":"test_password"}

response = opener.open(request, urllib.urlencode(data))

# send again, you will see cookie sent to web server
response = opener.open(request, urllib.urlencode(data))

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

本文出自https://www.cnblogs.com/40406-jun/p/6849710.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章