1 回答

TA貢獻1772條經(jīng)驗 獲得超5個贊
您無法直接獲取目錄列表,因為它是負(fù)責(zé)返回響應(yīng)的 HTTP 服務(wù)器,在某些情況下,您將獲得一個 HTML 頁面,顯示指向“目錄”內(nèi)所有文件的鏈接,如您的情況“http:/ /somehost/maindir/recent/" 將以 html 格式列出最近目錄中的所有 zip 文件。
一種解決方案可能是使用 Beautifulsoup 解析該 html 頁面并從該“最近”目錄頁面獲取所有指向 zip 文件的鏈接。
from bs4 import BeautifulSoup
import requests
url = 'http://somehost/maindir/recent/'
def get_files(url):
page = requests.get(url).text
soup = BeautifulSoup(page, 'html.parser')
return [url + '/' + node.get('href') for node in soup.find_all('a') if
node.get('href').endswith('.zip')]
file_links = get_files(url)
for zfile in file_links:
with RemoteZip(zfile) as zip:
for zip_info in zip.infolist():
data = zip.read(zip_info.filename)
添加回答
舉報