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)
添加回答
举报