Python下載檔案例項
Downloading files from the internet is something that almost every programmer will have to do at some point.
Python provides several ways to do just that in its standard library. Probably the most popular way to download a file is over HTTP using the urllib or urllib2 module.
Python also comes with ftplib for FTP downloads.
Finally there’s a new 3rd party module that’s getting a lot of buzz called requests. We’ll be focusing on the two urllib modules and requests for this article.
Since this is a pretty simple task, we’ll just show a quick and dirty script. that downloads the same file with each library and names the result slightly differently. We will download a zipped file from this very blog for our example script. Let’s take a look:
import urllib2
import requests
url = 'http://www.blog.pythonlibrary.org/wp-content/uploads/2012/06/wxDbViewer.zip'
print "downloading with urllib"
urllib.urlretrieve(url, "code.zip")
print "downloading with urllib2"
f = urllib2.urlopen(url)
data = f.read()
with open("code2.zip", "wb") as code:
code.write(data)
print "downloading with requests"
r = requests.get(url)
with open("code3.zip", "wb") as code:
code.write(r.content)
Python provides several ways to do just that in its standard library. Probably the most popular way to download a file is over HTTP using the urllib or urllib2 module.
Python also comes with ftplib for FTP downloads.
Finally there’s a new 3rd party module that’s getting a lot of buzz called requests. We’ll be focusing on the two urllib modules and requests for this article.
Since this is a pretty simple task, we’ll just show a quick and dirty script. that downloads the same file with each library and names the result slightly differently. We will download a zipped file from this very blog for our example script. Let’s take a look:
CODE:
import urllibimport urllib2
import requests
url = 'http://www.blog.pythonlibrary.org/wp-content/uploads/2012/06/wxDbViewer.zip'
print "downloading with urllib"
urllib.urlretrieve(url, "code.zip")
print "downloading with urllib2"
f = urllib2.urlopen(url)
data = f.read()
with open("code2.zip", "wb") as code:
code.write(data)
print "downloading with requests"
r = requests.get(url)
with open("code3.zip", "wb") as code:
code.write(r.content)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-732521/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- servlet檔案下載例項剖析Servlet
- python 下載檔案demoPython
- python之檔案下載Python
- Python xml.etree.ElementTree讀寫xml檔案例項PythonXML
- 轉化成Access檔案下載的例項
- Asp.net C# 檔案下載,附件下載程式碼案例,不顯示檔案路徑ASP.NETC#
- 【書籍】Django專案例項精解(第2版) pdf 下載Django
- editorconfig配置檔案例項
- qt 寫入xml檔案例項QTXML
- Python展示檔案下載進度條Python
- python下載檔案的三種方法Python
- 非歸檔模式下線上日誌檔案破壞後例項恢復案例模式
- ORACLE配置tnsnames.ora檔案例項Oracle
- python開發例項-python開發案例Python
- PHP+Mysql統計檔案下載次數例項PHPMySql
- JavaWeb之實現檔案上傳與下載例項JavaWeb
- 檔案下載
- 完整的python專案例項-python完整專案Python
- iOS開發網路篇之檔案下載、大檔案下載、斷點下載iOS斷點
- 00、下載檔案
- Ajax 下載檔案
- FastApi下載檔案ASTAPI
- MVC 下載檔案MVC
- js 檔案下載JS
- Servlet下載檔案Servlet
- httpWebRequest 檔案下載HTTPWeb
- php檔案下載PHP
- .net 檔案下載
- php 檔案下載PHP
- HttpClient 下載檔案HTTPclient
- Response下載檔案
- RAC環境下單例項啟動Oracle資料庫重建控制檔案案例單例Oracle資料庫
- Python介面自動化——檔案上傳/下載介面Python
- Python使用socket_TCP實現小檔案下載PythonTCP
- python下載模組Python
- Rollup處理並打包JS檔案專案例項JS
- 呼叫 WinSCP 下載遠端伺服器檔案 Python伺服器Python
- python寫的FTP簡單上傳下載檔案薦PythonFTP