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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python xml.etree.ElementTree讀寫xml檔案例項PythonXML
- python之檔案下載Python
- python 下載檔案demoPython
- qt 寫入xml檔案例項QTXML
- Asp.net C# 檔案下載,附件下載程式碼案例,不顯示檔案路徑ASP.NETC#
- 【書籍】Django專案例項精解(第2版) pdf 下載Django
- python開發例項-python開發案例Python
- Python展示檔案下載進度條Python
- 完整的python專案例項-python完整專案Python
- PHP+Mysql統計檔案下載次數例項PHPMySql
- JavaWeb之實現檔案上傳與下載例項JavaWeb
- 檔案下載
- Python使用socket_TCP實現小檔案下載PythonTCP
- Python介面自動化——檔案上傳/下載介面Python
- 完整的python專案例項-Python例項練手專案彙總(附原始碼)Python原始碼
- Rollup處理並打包JS檔案專案例項JS
- 00、下載檔案
- Ajax 下載檔案
- js 檔案下載JS
- HttpClient 下載檔案HTTPclient
- JAVA檔案下載Java
- Response下載檔案
- FastApi下載檔案ASTAPI
- python互動式專案例項 外星人入侵Python
- PHP引入合併多個JS和CSS檔案例項PHPJSCSS
- Python 下載圖片Python
- python下載包(pycharm)PythonPyCharm
- python 各版本下載Python
- python下載模組Python
- 檔案上傳下載
- Flutter 下載檔案操作Flutter
- 檔案程式設計、檔案下載程式設計
- 原生PHP網頁匯出和匯入excel檔案例項PHP網頁Excel
- python 國內下載地址Python
- Python Selenium安裝下載Python
- 檔案下載(URL,文件流)
- CentOS 7映象檔案下載CentOS
- 檔案下載那點事
- 下載網頁音原始檔網頁