
- Python - 網路程式設計
- Python - 網路簡介
- Python - 網路環境
- Python - 網際網路協議
- Python - IP 地址
- Python - DNS 查詢
- Python - 路由
- Python - HTTP 請求
- Python - HTTP 響應
- Python - HTTP 標頭
- Python - 自定義 HTTP 請求
- Python - 請求狀態程式碼
- Python - HTTP 身份驗證
- Python - HTTP 資料下載
- Python - 連線重用
- Python - 網路介面
- Python - 套接字程式設計
- Python - HTTP 客戶端
- Python - HTTP 伺服器
- Python - 構建 URL
- Python - Web 表單提交
- Python - 資料庫和 SQL
- Python - Telnet
- Python - 電子郵件
- Python - SMTP
- Python - POP3
- Python - IMAP
- Python - SSH
- Python - FTP
- Python - SFTP
- Python - Web 伺服器
- Python - 資料上傳
- Python - 代理伺服器
- Python - 目錄列表
- Python - 遠端過程呼叫
- Python - RPC JSON 伺服器
- Python - Google 地圖
- Python - RSS 源
Python - HTTP 資料下載
我們可以使用處理 FTP 或檔案傳輸協議的 Python 模組從伺服器下載資料。我們還可以讀取資料,然後將其儲存到本地系統。
我們需要安裝模組 ftplib 來實現此目的。
pip install ftplib
獲取檔案
我們可以透過使用 getfile 方法獲取特定的檔案。此方法會將檔案副本從遠端系統移動到發起 FTP 連線的本地系統。
import ftplib import sys def getFile(ftp, filename): try: ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write) except: print "Error" ftp = ftplib.FTP("ftp.nluug.nl") ftp.login("anonymous", "ftplib-example-1") ftp.cwd('/pub/') change directory to /pub/ getFile(ftp,'README.nluug') ftp.quit()
當我們執行上述程式時,會發現檔案 README.nlug 存在於發起連線的本地系統中。
讀取資料
在下面的示例中,我們使用模組 urllib2 讀取所需的資料部分,我們可以對其進行復制並將其儲存到本地系統。
當我們執行上述程式時,會獲得以下輸出−
import urllib2 response = urllib2.urlopen('https://tutorialspoint.tw/python') html = response.read(200) print html
當我們執行上述程式時,會獲得以下輸出−
<!DOCTYPE html> <!--[if IE 8]><html class="ie ie8"> <![endif]--> <!--[if IE 9]><html class="ie ie9"> <![endif]--> <!--[if gt IE 9]><!--> <html> <!--<![endif]--> <head> <!-- Basic --> <meta charset="ut
廣告