如果我只有 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/