亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

遍歷索引目錄的服務器 URL 并讀取文件

遍歷索引目錄的服務器 URL 并讀取文件

哈士奇WWW 2023-06-06 17:26:50
http服務器上有一個目錄,其url是http://somehost/maindir/recent/。這個“最近”目錄包含 50 個 zip 子目錄。我可以讀取一個 zip 文件 zfile = "http://somehost/maindir/recent/1.zip" with RemoteZip(zfile) as zip:        for zip_info in zip.infolist():            data = zip.read(zip_info.filename)但是我不知道要遍歷“http://somehost/maindir/recent/”并從每個 zip 中讀取數據。我嘗試了 glob、os.join、os.walk 但在靜脈中。我想要這樣的東西:for zfile in baseurl: //unable to do this line.    with RemoteZip(zfile) as zip:        for zip_info in zip.infolist():                data = zip.read(zip_info.filename)
查看完整描述

1 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

您無法直接獲取目錄列表,因為它是負責返回響應的 HTTP 服務器,在某些情況下,您將獲得一個 HTML 頁面,顯示指向“目錄”內所有文件的鏈接,如您的情況“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)


查看完整回答
反對 回復 2023-06-06
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號