Python 列出 HTTP 文件和目录

标签 python html directory ip-address

如果我只有 IP 地址,如何列出文件和文件夹?

使用 urllib 和其他,我只能显示 index.html 文件的内容。但是,如果我还想查看根目录中有哪些文件怎么办?

我正在寻找一个示例,说明如何在需要时实现用户名和密码。 (大多数时候 index.html 是公开的,但有时其他文件不是)。

最佳答案

使用requests获取页面内容,使用BeautifulSoup解析结果。
例如,如果我们在 http://cdimage.debian.org/debian-cd/8.2.0-live/i386/iso-hybrid/ 搜索所有 iso 文件:

from bs4 import BeautifulSoup
import requests

url = 'http://cdimage.debian.org/debian-cd/8.2.0-live/i386/iso-hybrid/'
ext = 'iso'

def listFD(url, ext=''):
    page = requests.get(url).text
    print page
    soup = BeautifulSoup(page, 'html.parser')
    return [url + '/' + node.get('href') for node in soup.find_all('a') if node.get('href').endswith(ext)]

for file in listFD(url, ext):
    print file

关于Python 列出 HTTP 文件和目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11023530/

相关文章:

python - 使用 Cloud Scheduler 的 HTTP 触发 Cloud Function

javascript - 对特定的 PHP 函数进行 jQuery AJAX 调用

javascript - 如何实时更新 localStorage.length() 上的数据

javascript - 用于 HTML 表单的 XY slider Controller 网格 用于网络和移动网络的 jQuery/Javascript

c++ - C++中如何检查文件夹是否有写权限

powershell - Get-Childitem目录通配符 “Access is denied”

带有 "Position only parameter"的 Python 函数

python - 在Python中从html代码href解析页面

c - 确定目录中最旧的文件

python - 在事务中强制提交嵌套的 save()