Python列出HTTP文件和目录 [英] Python to list HTTP-files and directories

查看:112
本文介绍了Python列出HTTP文件和目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I list files and folders if I only have an IP-address?

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

With urllib and others, I am only able to display the content of the index.html file. But what if I want to see which files are in the root as well?

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

I am looking for an example that shows how to implement username and password if needed. (Most of the time index.html is public, but sometimes the other files are not).

推荐答案

使用请求以获取页面内容,而 BeautifulSoup 来解析结果。

例如,如果我们搜索所有<$位于 http://cdimage.debian.org/debian-cd/8.2.0-live/i386/iso-hybrid/ iso 个文件$ c>:

Use requests to get page content and BeautifulSoup to parse the result.
For example if we search for all iso files at http://cdimage.debian.org/debian-cd/8.2.0-live/i386/iso-hybrid/:

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文件和目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆