[python]-urllib和urllib2模塊

轉自:http://zeping.blog.51cto.com/6140112/1143722html

urllib 和urllib2都是接受URL請求的相關模塊,可是提供了不一樣的功能。python

urllib提供urlencode方法用來GET查詢字符串的產生,而urllib2沒有。web

urllib:緩存

網頁基礎:服務器

  1. import urllib 
  2. #打開51cto 
  3. cto = urllib.urlopen('http://www.51cto.com') 
  4. #打開本地文件:cto = urllib.urlopen(url='file:/root/python/ulib')
  5. #打開ftp:cto = = urllib.urlopen(url='ftp://用戶名:密碼@ftp地址/')
  6. #讀取51cto首頁的代碼 
  7. print cto.read() 
  8. #獲取遠程服務器返回的頭信息,跟curl -I www,51cto.com差很少 
  9. print cto.info() 
  10. #返回http狀態碼,200表示成功,404表示網址未找到 
  11. print cto.getcode() 
  12. #返回請求的URL 
  13. print cto.geturl() 
  14. #運行結果 
  15. [root@localhost python]#python ctourl 
  16. 。。。。。。。省略cto.read()函數,輸出太多啦。。。。。 
  17. Server: Tengine  #cto.info()返回信息 
  18. Date: Wed, 27 Feb 2013 15:05:46 GMT 
  19. Content-Type: text/html 
  20. Connection: close 
  21. Vary: Accept-Encoding 
  22. Load-Balancing: web48 
  23.  
  24. 200              #cto.getcode()返回信息 
  25. http://www.51cto.com   #cto.geturl()返回信息 
  26. #urlopen返回的是一個類文件對象,而這個對象的使用方法和文件對象的 
  27. #使用方法徹底同樣。 

 字符的編碼和解碼:curl

 

  1. import urllib,os 
  2. #對字符串進行編碼 
  3. stra = urllib.quote('this is python') 
  4. print stra 
  5. #對字符串進行解碼 
  6. print urllib.unquote(stra) 
  7. #這個方法用‘+’代替了%20 和urllib.quote相似, 
  8. strb = urllib.quote_plus('this is python') 
  9. print strb 
  10. #解碼 
  11. print urllib.unquote_plus(strb) 
  12.  
  13. dicta = {'name':'zeping','passwd':'123456'} 
  14. #urlencode將字典轉換成url參數 
  15. print urllib.urlencode(dicta) 
  16.  
  17. #將本地路徑轉換成url路徑 
  18. filename = urllib.pathname2url('/python/test.py') 
  19. print filename 
  20. #將url路徑轉換成本地路徑 
  21. print urllib.url2pathname(filename) 
  22.  
  23. ##########運行結果########## 
  24. [root@localhost python]# python quote 
  25. this%20is%20python 
  26. this is python 
  27. this+is+python 
  28. this is python 
  29. passwd=123456&name=zeping 
  30. /python/test.py 
  31. /python/test.py 

urllib.urlretrieve():下載函數

  1. import urllib 
  2. def Schedule(a,b,c): 
  3.     ''''' 
  4.     a:已經下載的數據塊 
  5.     b:數據塊的大小 
  6.     c:遠程文件的大小 
  7.    ''' 
  8.     per = 100.0 * a * b / c 
  9.     if per > 100 : 
  10.         per = 100 
  11.     print '%.2f%%' % per 
  12.  
  13. #這裏如下載緩存插件爲例 
  14. url = 'http://fastlnmp.googlecode.com/files/eaccelerator-0.9.6.tar.bz2' 
  15. #獲取文件名,這裏是下載到當前目錄下,若果要下載到別的目錄必 
  16. #須輸入絕對路徑和文件名字:/root/tools/eaccelerator-0.9.6.tar.bz2 
  17. local = url.split('/')[-1] 
  18. urllib.urlretrieve(url,local,Schedule) 
  19. #########運行結果########## 
  20. [root@localhost urllib]# python down 
  21. 0.00% 
  22. 7.74% 
  23. 15.48% 
  24. 23.22% 
  25. 30.96% 
  26. 38.70% 
  27. 46.44% 
  28. 54.18% 
  29. 61.92% 
  30. 69.66% 
  31. 77.40% 
  32. 85.15% 
  33. 92.89% 
  34. 100.00% 
  35. [root@localhost urllib]# ls 
  36. down  eaccelerator-0.9.6.tar.bz2  ulib2 

urllib2:this

urllib2能夠接受一個Request類的實例來設置URL請求的headers,urllib僅能夠接受URL。google

以上轉自:http://zeping.blog.51cto.com/6140112/1143722編碼

相關文章
相關標籤/搜索