如何从远程服务器检索文件目录?

发布于 2024-08-10 07:10:03 字数 115 浏览 1 评论 0原文

如果我在远程 Web 服务器上有一个允许目录浏览的目录,我将如何从其他 Web 服务器获取其中列出的所有文件?我知道我可以使用 urllib2.urlopen 来获取单个文件,但是如何获取该远程目录中所有文件的列表?

If I have a directory on a remote web server that allows directory browsing, how would I go about to fetch all those files listed there from my other web server? I know I can use urllib2.urlopen to fetch individual files, but how would I get a list of all the files in that remote directory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-08-17 07:10:03

如果网络服务器启用了目录浏览,它将返回一个 HTML 文档,其中包含所有文件的链接。您可以解析 HTML 文档并提取所有链接。这将为您提供文件列表。

您可以使用 HTMLParser 类来提取您感兴趣的元素。类似这会起作用:

from HTMLParser import HTMLParser
import urllib

class AnchorParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
            if tag =='a':
                    for key, value in attrs.iteritems()):
                            if key == 'href':
                                    print value

parser = AnchorParser()
data = urllib.urlopen('http://somewhere').read()
parser.feed(data)

If the webserver has directory browsing enabled, it will return a HTML document with links to all the files. You could parse the HTML document and extract all the links. This would give you the list of files.

You can use the HTMLParser class to extract the elements you're interested in. Something like this will work:

from HTMLParser import HTMLParser
import urllib

class AnchorParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
            if tag =='a':
                    for key, value in attrs.iteritems()):
                            if key == 'href':
                                    print value

parser = AnchorParser()
data = urllib.urlopen('http://somewhere').read()
parser.feed(data)
强者自强 2024-08-17 07:10:03

为什么不使用 curlwget 递归下载给定页面,并限制最多 1 级。您将省去编写脚本的所有麻烦。

例如类似的东西

wget -H -r --level=1 -k -p www.yourpage/dir

Why don't you use curl or wget to recursively download the given page, and limit it upto 1 level. You will save all the trouble of writing the script.

e.g. something like

wget -H -r --level=1 -k -p www.yourpage/dir
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文