簡體   English   中英

使用Selenium,Firefox,Python在自動點擊下載鏈接后保存EPS文件下載到磁盤

[英]using Selenium, Firefox, Python to save download of EPS files to disk after automated clicking of download link

工具:Ubuntu,Python,Selenium,Firefox

我想要自動從訂閱網站下載圖像文件。 除了付費訂閱之外,我無法訪問服務器。 為了避免為每個文件下載單擊按鈕,我決定使用Python,Selenium和Firefox自動化它。 (我這兩天第一次一起使用這三個。我對cookies也知之甚少。)

我有興趣按順序或偏好下載以下三種格式:['EPS','PNG','JPG']。 網站上提供了每種格式的按鈕。

通過手動設置Firefox首選項,我已成功地自動將'PNG'和'JPG'文件下載到磁盤上,如本文所述: python webcrawler下載文件

但是,當文件采用“EPS”格式時,“您已選擇保存”對話框仍會在Firefox窗口中彈出。

從我的代碼中可以看出,我已經設置了將'EPS'文件保存到磁盤的首選項。 (同樣,'JPG'和'PNG'文件按預期保存。)

from selenium import webdriver

profile = webdriver.firefox.firefox_profile.FirefoxProfile()
profile.set_preference("browser.download.folderList", 1)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',
                       'image/jpeg,image/png,application/postscript,'
                       'application/eps,application/x-eps,image/x-eps,'
                       'image/eps')
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("plugin.disable_full_page_plugin_for_types",
                       "application/eps,application/x-eps,image/x-eps,"
                       "image/eps")
profile.set_preference(
    "general.useragent.override",
    "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:26.0)"
    " Gecko/20100101 Firefox/26.0")
driver = webdriver.Firefox(firefox_profile=profile)

#I then log in and begin automated clicking to download files. 'JPG' and 'PNG' files are
#saved to disk as expected. The 'EPS' files present a save dialog box in Firefox.

我嘗試安裝一個名為“download-statusbar”的Firefox擴展程序,聲稱會否定出現任何保存對話框。 擴展程序在Selenium Firefox瀏覽器中加載,但它不起作用。 (很多評論說,盡管開發人員堅持認為它確實有效,但擴展仍然被破壞了。)無論如何它都不適用於我,所以我放棄了它。

我在此嘗試中將此添加到Firefox配置文件中:

#The extension loads, but it doesn't function.
download_statusbar = '/home/$USER/Downloads/'
                     '/download_statusbar_fixed-1.2.00-fx.xpi'
profile.add_extension(download_statusbar)

從閱讀其他stackoverflow.com帖子,我決定看看我是否可以通過URL與urllib2下載文件。 據我了解這是如何工作的,我需要在標題中添加cookie,以便通過URL驗證“EPS”文件的下載。

我不熟悉這種技術,但這里是我試圖直接下載文件的代碼。 盡管我試圖在urllib2開啟者中設置cookie,但它仍然失敗了'403 Forbidden'響應。

import urllib2
import cookielib
import logging
import sys

cookie_jar = cookielib.LWPCookieJar()
handlers = [
    urllib2.HTTPHandler(),
    urllib2.HTTPSHandler(),
]
[h.set_http_debuglevel(1) for h in handlers]
handlers.append(urllib2.HTTPCookieProcessor(cookie_jar))
#using selenium driver cookies, returns a list of dictionaries
cookies = driver.get_cookies()
opener = urllib2.build_opener(*handlers)
opener.addheaders = [(
    'User-agent',
    'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:26.0) '
    'Gecko/20100101 Firefox/26.0'
)]
logger = logging.getLogger("cookielib")
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.DEBUG)
for item in cookies:
    opener.addheaders.append(('Cookie', '{}={}'.format(
        item['name'], item['value']
    )))
    logger.info('{}={}'.format(item['name'], item['value']))
response = opener.open('http://path/to/file.eps')
#Fails with a 403 Forbidden response

有什么想法或建議嗎? 我是否遺漏了一些簡單的內容,或者我是否需要放棄自動下載EPS文件的希望? 提前致謝。

感謝@unutbu幫我解決這個問題。 我只是不明白文件下載的解剖。 我現在明白了一點。

我最終在Firefox上安裝了一個名為“Live HTTP Headers”的擴展來檢查服務器發送的標頭。 事實證明,'EPS'文件是以'application-type'為'application / octet-stream'發送的。

現在EPS文件按預期保存到磁盤。 我將Firefox首選項修改為以下內容:

profile.set_preference('browser.helperApps.neverAsk.saveToDisk',
                   'image/jpeg,image/png,'
                   'application/octet-stream')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM