-2

我正在寻找一种从不同页面下载文件并将它们存储在本地计算机的特定文件夹下的方法。我正在使用 Python 2.7

请参阅以下字段:

文件类型字段

编辑

这是html内容:

<input type="hidden" name="supplier.orgProfiles(1152444).location.locationPurposes().extendedAttributes(Upload_RFI_Form).value.filename" value="Screenshot.docx">

<a style="display:inline; position:relative;" href="

                                      /aems/file/filegetrevision.do?fileEntityId=8120070&cs=LU31NT9us5P9Pvkb1BrtdwaCrEraskiCJcY6E2ucP5s.xyz">
                                Screenshot.docx
                             </a>

我刚刚尝试过的一种可能性:如果添加 html 内容https://xyz.test.com并构造如下所示的 URL

https://xyz.test.com/aems/file/filegetrevision.do?fileEntityId=8120070&cs=LU31NT9us5P9Pvkb1BrtdwaCrEraskiCJcY6E2ucP5s.xyz

并将该 URL 放在浏览器上,然后点击Enter让我有机会下载文件,如屏幕截图所述。但是现在我们能找到这样aems/file/filegetrevision.do?fileEntityId=8120070&cs=LU31NT9us5P9Pvkb1BrtdwaCrEraskiCJcY6E2ucP5s.xyz的值吗?它在那里存在多少?

编码 我到现在为止的尝试

只是痛苦如何下载该文件。使用脚本构造的 URL:

for a in soup.find_all('a', {"style": "display:inline; position:relative;"}, href=True):
    href = a['href'].strip()
    href = "https://xyz.test.com/" + href
print(href)

请在这里帮助我!

如果你们需要我提供更多信息,请告诉我,我很乐意与你们分享。

提前致谢!

4

2 回答 2

2

正如@JohnZwinck 建议的那样,您可以使用urllib.urlretrievere模块在给定页面上创建链接列表并下载每个文件。下面是一个例子。

#!/usr/bin/python

"""
This script would scrape and download files using the anchor links.
"""


#Imports

import os, re, sys
import urllib, urllib2

#Config
base_url = "http://www.google.com/"
destination_directory = "downloads"


def _usage():
    """
    This method simply prints out the Usage information.
    """

    print "USAGE: %s <url>" %sys.argv[0]


def _create_url_list(url):
    """
    This method would create a list of downloads, using the anchor links
    found on the URL passed.
    """

    raw_data = urllib2.urlopen(url).read()
    raw_list = re.findall('<a style="display:inline; position:relative;" href="(.+?)"', raw_data)
    url_list = [base_url + x for x in raw_list]
    return url_list


def _get_file_name(url):
    """
    This method will return the filename extracted from a passed URL
    """

    parts = url.split('/')
    return parts[len(parts) - 1]


def _download_file(url, filename):
    """
    Given a URL and a filename, this method will save a file locally to the»
    destination_directory path.
    """
    if not os.path.exists(destination_directory):
        print 'Directory [%s] does not exist, Creating directory...' % destination_directory
        os.makedirs(destination_directory)
    try:
        urllib.urlretrieve(url, os.path.join(destination_directory, filename))
        print 'Downloading File [%s]' % (filename)
    except:
        print 'Error Downloading File [%s]' % (filename)


def _download_all(main_url):
    """
    Given a URL list, this method will download each file in the destination
    directory.
    """

    url_list = _create_url_list(main_url)
    for url in url_list:
        _download_file(url, _get_file_name(url))


def main(argv):
    """
    This is the script's launcher method.
    """

    if len(argv) != 1:
        _usage()
        sys.exit(1)
    _download_all(sys.argv[1])
    print 'Finished Downloading.'


if __name__ == '__main__':
    main(sys.argv[1:])

您可以根据需要更改 和 并将脚本另存base_url为. 然后从终端使用它就像destination_directorydownload.py

python download.py http://www.example.com/?page=1
于 2013-01-07T18:56:35.667 回答
1

我们不知道您从哪个服务获得了第一张图片,但我们假设它位于某种网站上——可能是您公司内部的网站。

您可以尝试的最简单的方法是使用urllib.urlretrieve根据其 URL “获取”文件。如果您可以右键单击该页面上的链接、复制 URL 并将其粘贴到您的代码中,您也许可以执行此操作。

但是,这可能不起作用,例如,如果在访问该页面之前需要进行复杂的身份验证。您可能需要编写实际执行登录的 Python 代码(就像用户在控制它,输入密码一样)。如果你走得那么远,你应该把它作为一个单独的问题发布。

于 2013-01-07T12:02:40.057 回答