[轉]python的httplib、urllib和urllib2的區別及用

原文連接:http://blog.csdn.net/dolphin_h/article/details/45296353php

慢慢的把它們總結一下,總結就是最好的學習方法html

宗述

首先來看一下他們的區別python

urllib和urllib2ajax

urllib 和urllib2都是接受URL請求的相關模塊,可是urllib2能夠接受一個Request類的實例來設置URL請求的headers,urllib僅能夠接受URL。瀏覽器

這意味着,你不能夠假裝你的User Agent字符串等。服務器

urllib提供urlencode方法用來GET查詢字符串的產生,而urllib2沒有。這是爲什麼urllib常和urllib2一塊兒使用的緣由。cookie

目前的大部分http請求都是經過urllib2來訪問的app

 

httplibpost

httplib實現了HTTP和HTTPS的客戶端協議,通常不直接使用,在python更高層的封裝模塊中(urllib,urllib2)使用了它的http實現。學習

 

urllib簡單用法

urllib.urlopen(url[, data[, proxies]]) :

[python]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. google = urllib.urlopen('http://www.google.com')  
  2. print 'http header:/n', google.info()  
  3. print 'http status:', google.getcode()  
  4. print 'url:', google.geturl()  
  5. for line in google: # 就像在操做本地文件  
  6.     print line,  
  7. google.close()  


詳細使用方法見

urllib學習

 

urllib2簡單用法

最簡單的形式

[python]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. import urllib2  
  2.    response=urllib2.urlopen('http://www.douban.com')  
  3.    html=response.read()  

實際步驟:

一、urllib2.Request()的功能是構造一個請求信息,返回的req就是一個構造好的請求

二、urllib2.urlopen()的功能是發送剛剛構造好的請求req,並返回一個文件類的對象response,包括了全部的返回信息。

三、經過response.read()能夠讀取到response裏面的html,經過response.info()能夠讀到一些額外的信息。

以下:

[python]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. #!/usr/bin/env python  
  2.     import urllib2  
  3.     req = urllib2.Request("http://www.douban.com")  
  4.     response = urllib2.urlopen(req)  
  5.     html = response.read()  
  6.     print html  

有時你會碰到,程序也對,可是服務器拒絕你的訪問。這是爲何呢?問題出在請求中的頭信息(header)。 有的服務端有潔癖,不喜歡程序來觸摸它。這個時候你須要將你的程序假裝成瀏覽器來發出請求。請求的方式就包含在header中。
常見的情形:

[python]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. import urllib  
  2. import urllib2  
  3. url = 'http://www.someserver.com/cgi-bin/register.cgi'  
  4. user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'# 將user_agent寫入頭信息  
  5. values = {'name' : 'who','password':'123456'}  
  6. headers = { 'User-Agent' : user_agent }  
  7. data = urllib.urlencode(values)  
  8. req = urllib2.Request(url, data, headers)  
  9. response = urllib2.urlopen(req)  
  10. the_page = response.read()  

 

values是post數據

GET方法

例如百度:

百度是經過http://www.baidu.com/s?wd=XXX 來進行查詢的,這樣咱們須要將{‘wd’:’xxx’}這個字典進行urlencode

[python]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. #coding:utf-8  
  2. import urllib   
  3. import urllib2    
  4. url = 'http://www.baidu.com/s'   
  5. values = {'wd':'D_in'}     
  6. data = urllib.urlencode(values)  
  7. print data   
  8. url2 = url+'?'+data  
  9. response = urllib2.urlopen(url2)    
  10. the_page = response.read()   
  11. print the_page  

 

POST方法

[python]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. import urllib  
  2. import urllib2  
  3. url = 'http://www.someserver.com/cgi-bin/register.cgi'  
  4. user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' //將user_agent寫入頭信息  
  5. values = {'name' : 'who','password':'123456'}      //post數據  
  6. headers = { 'User-Agent' : user_agent }  
  7. data = urllib.urlencode(values)                   //對post數據進行url編碼  
  8. req = urllib2.Request(url, data, headers)  
  9. response = urllib2.urlopen(req)  
  10. the_page = response.read()  

urllib2帶cookie的使用

[python]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. #coding:utf-8  
  2. import urllib2,urllib  
  3. import cookielib  
  4.    
  5. url = r'http://www.renren.com/ajaxLogin'  
  6.    
  7. #建立一個cj的cookie的容器  
  8. cj = cookielib.CookieJar()  
  9. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))  
  10. #將要POST出去的數據進行編碼  
  11. data = urllib.urlencode({"email":email,"password":pass})  
  12. r = opener.open(url,data)  
  13. print cj  

 

httplib簡單用法

簡單示例

[python]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. #!/usr/bin/env python      
  2. # -*- coding: utf-8 -*-      
  3. import httplib    
  4. import urllib    
  5.     
  6. def sendhttp():    
  7.     data = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})       
  8.     headers = {"Content-type": "application/x-www-form-urlencoded",    
  9.                "Accept": "text/plain"}    
  10.     conn = httplib.HTTPConnection('bugs.python.org')    
  11.     conn.request('POST', '/', data, headers)    
  12.     httpres = conn.getresponse()    
  13.     print httpres.status    
  14.     print httpres.reason    
  15.     print httpres.read()               
  16.                   
  17. if __name__ == '__main__':      
  18.     sendhttp()  


具體用法見

httplib模塊

相關文章
相關標籤/搜索