參考博客:https://www.cnblogs.com/TankXiao/p/3081312.htmlhtml
http://blog.csdn.net/youfuchen/article/details/19492821nginx
如下是學習筆記:app
Get方法, 而且自定義headerpost
# -* - 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()
HTTPErrorurl
默認狀況下,只要不是2XX的返回碼,都會被當成錯誤對待。能夠經過HTTPError來捕捉錯誤信息spa
import urllib2 url = "http://www.jnrain.com/go" try: response = urllib2.urlopen(url) print response.info() print response.read() except urllib2.HTTPError, e: print e.getcode() print e.reason print e.geturl() print "--" *100 print e.info() print e.read()
經過捕捉錯誤能夠打印出詳細的錯誤信息:.net
404 Not Found http://www.jnrain.com/go ------------------------- Server: nginx/1.4.1 Date: Wed, 19 Feb 2014 08:51:50 GMT Content-Type: text/html; charset=gb18030 Content-Length: 168 Connection: close <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.4.1</center> </body> </html>