1. 程式人生 > >python的httplib、urllib和urllib2的區別及用

python的httplib、urllib和urllib2的區別及用

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

宗述

首先來看一下他們的區別

urllib和urllib2

urllib 和urllib2都是接受URL請求的相關模組,但是urllib2可以接受一個Request類的例項來設定URL請求的headers,urllib僅可以接受URL。

這意味著,你不可以偽裝你的User Agent字串等。

urllib提供urlencode方法用來GET查詢字串的產生,而urllib2沒有。這是為何urllib常和urllib2一起使用的原因。

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

httplib

httplib實現了HTTP和HTTPS的客戶端協議,一般不直接使用,在更高層的封裝模組中(urllib,urllib2)使用了它的http實現。

urllib簡單用法

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

google = urllib.urlopen('http://www.google.com')
print 'http header:/n', google.info()
print 'http status:', google.getcode()
print 'url:', google.geturl()
for line in google: # 就像在操作本地檔案
    print line,
google.close()


詳細使用方法見

urllib2簡單用法

最簡單的形式
 import urllib2
    response=urllib2.urlopen('http://www.douban.com')
    html=response.read()

實際步驟:

1urllib2.Request()的功能是構造一個請求資訊,返回的req就是一個構造好的請求

2、urllib2.urlopen()的功能是傳送剛剛構造好的請求req,並返回一個檔案類的物件response,包括了所有的返回資訊。

3通過response.read()可以讀取到response裡面的html,通過response.info()可以讀到一些額外的資訊

如下:

#!/usr/bin/env python
    import urllib2
    req = urllib2.Request("http://www.douban.com")
    response = urllib2.urlopen(req)
    html = response.read()
    print html


有時你會碰到,程式也對,但是伺服器拒絕你的訪問。這是為什麼呢?問題出在請求中的頭資訊(header)。 有的服務端有潔癖,不喜歡程式來觸控它。這個時候你需要將你的程式偽裝成瀏覽器來發出請求。請求的方式就包含在header中。
常見的情形:
import urllib
import urllib2
url = 'http://www.someserver.com/cgi-bin/register.cgi'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'# 將user_agent寫入頭資訊
values = {'name' : 'who','password':'123456'}
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()

values是post資料

GET方法

例如百度:

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

#coding:utf-8
import urllib 
import urllib2  
url = 'http://www.baidu.com/s' 
values = {'wd':'D_in'}   
data = urllib.urlencode(values)
print data 
url2 = url+'?'+data
response = urllib2.urlopen(url2)  
the_page = response.read() 
print the_page

POST方法

import urllib
import urllib2
url = 'http://www.someserver.com/cgi-bin/register.cgi'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' //將user_agent寫入頭資訊
values = {'name' : 'who','password':'123456'}      //post資料
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)                   //對post資料進行url編碼
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()

urllib2帶cookie的使用

#coding:utf-8
import urllib2,urllib
import cookielib
 
url = r'http://www.renren.com/ajaxLogin'
 
#建立一個cj的cookie的容器
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#將要POST出去的資料進行編碼
data = urllib.urlencode({"email":email,"password":pass})
r = opener.open(url,data)
print cj

httplib簡單用法

簡單示例

#!/usr/bin/env python    
# -*- coding: utf-8 -*-    
import httplib  
import urllib  
  
def sendhttp():  
    data = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})     
    headers = {"Content-type": "application/x-www-form-urlencoded",  
               "Accept": "text/plain"}  
    conn = httplib.HTTPConnection('bugs.python.org')  
    conn.request('POST', '/', data, headers)  
    httpres = conn.getresponse()  
    print httpres.status  
    print httpres.reason  
    print httpres.read()             
                
if __name__ == '__main__':    
    sendhttp()

具體用法見