【问题标题】:Python to list HTTP-files and directoriesPython 列出 HTTP 文件和目录
【发布时间】:2012-06-16 22:06:51
【问题描述】:

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

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

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

【问题讨论】:

  • 这篇文章“stackoverflow.com/questions/4589241/…>”应该对你有所帮助......希望:)

标签: python html directory ip-address


【解决方案1】:

Zety 提供了一个不错的紧凑型解决方案。我将通过使requests 组件更健壮和功能更强大来添加他的示例:

import requests
from bs4 import BeautifulSoup

def get_url_paths(url, ext='', params={}):
    response = requests.get(url, params=params)
    if response.ok:
        response_text = response.text
    else:
        return response.raise_for_status()
    soup = BeautifulSoup(response_text, 'html.parser')
    parent = [url + node.get('href') for node in soup.find_all('a') if node.get('href').endswith(ext)]
    return parent

url = 'http://cdimage.debian.org/debian-cd/8.2.0-live/i386/iso-hybrid'
ext = 'iso'
result = get_url_paths(url, ext)
print(result)

【讨论】:

    【解决方案2】:

    您可以使用以下脚本来获取 HTTP 服务器中子目录和目录中所有文件的名称。可以使用文件编写器来下载它们。

    from urllib.request import Request, urlopen, urlretrieve
    from bs4 import BeautifulSoup
    def read_url(url):
        url = url.replace(" ","%20")
        req = Request(url)
        a = urlopen(req).read()
        soup = BeautifulSoup(a, 'html.parser')
        x = (soup.find_all('a'))
        for i in x:
            file_name = i.extract().get_text()
            url_new = url + file_name
            url_new = url_new.replace(" ","%20")
            if(file_name[-1]=='/' and file_name[0]!='.'):
                read_url(url_new)
            print(url_new)
    
    read_url("www.example.com")
    

    【讨论】:

      【解决方案3】:

      使用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
      

      【讨论】:

        【解决方案4】:

        正如另一个答案所说,您无法直接通过 HTTP 获取目录列表。是 HTTP 服务器“决定”给你什么。有些会给你一个 HTML 页面,显示“目录”内所有文件的链接,有些会给你一些页面(index.html),有些甚至不会将“目录”解释为一个。

        例如,您可能有一个指向“http://localhost/user-login/”的链接:这并不意味着在服务器的文档根目录中有一个名为 user-login 的目录。服务器将其解释为指向某个页面的“链接”。

        现在,要实现您想要的,您要么必须使用 HTTP 以外的其他东西(您要访问的“IP 地址”上的 FTP 服务器就可以完成这项工作),或者在该机器上设置一个 HTTP 服务器为每个路径(http://192.168.2.100/directory)提供其中的文件列表(以任何格式)并通过 Python 解析。

        如果服务器提供“/bla/bla 索引”类型的页面(如 Apache 服务器所做的目录列表),您可以解析 HTML 输出以找出文件和目录的名称。如果不是(例如,自定义 index.html、或服务器决定为您提供的任何内容),那么您就不走运了 :(,您做不到。

        【讨论】:

        • 很好的答案。谢谢你的帮助。实际上,index.html 给了我其他文件和文件夹的名称。也许我可以尝试获取这些。
        • 太棒了 :) 如果是这种情况,请尝试使用 BeautifulSoup 在 python 中搜索 html 解析。还有其他的库。
        【解决方案5】:

        HTTP 不适用于“文件”和“目录”。选择不同的协议。

        【讨论】:

          猜你喜欢
          • 2011-02-23
          • 1970-01-01
          • 2014-02-02
          • 2015-08-22
          • 2011-09-04
          • 2014-10-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多