-2

さまざまなページからファイルをダウンロードして、ローカルマシンの特定のフォルダーに保存する方法を探しています。Python2.7を使用しています

以下のフィールドを参照してください。

Filetypefield

編集

ここに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>

私が試した1つの可能性: HTMLコンテンツを使用して、sayhttps://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.urlretrieve使用reして、特定のページにリンクのリストを作成し、各ファイルをダウンロードできます。以下に例を示します。

#!/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

最初の画像をどのサービスから入手したかはわかりませんが、ある種のWebサイト(おそらく社内のWebサイト)にあると想定します。

試すことができる最も簡単な方法は、urllib.urlretrieveを使用して、そのURLに基​​づいてファイルを「取得」することです。そのページのリンクを右クリックし、URLをコピーしてコードに貼り付けることができれば、これを実行できる可能性があります。

ただし、たとえば、そのページにアクセスする前に複雑な認証が必要な場合は、それが機能しない可能性があります。実際にログインを実行するPythonコードを作成する必要がある場合があります(ユーザーがログインを制御しているかのように、パスワードを入力します)。そこまで進んだら、別の質問として投稿する必要があります。

于 2013-01-07T12:02:40.057 に答える