我想下載一個來自Online-Magazin的PDF。要打開它,必須先登錄。然後打開PDF並下載它。如何使用Python 3中的Selenium Module(FireFox)下載PDF
以下是我的代碼。它可以登錄到頁面並且PDF也可以打開。但是PDF不能下載,因爲我不確定如何模擬點擊保存。我使用FireFox。
import os, time
from selenium import webdriver
from bs4 import BeautifulSoup
# Use firefox dowmloader to get file
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", 'D:/eBooks/Stocks_andCommodities/2008/Jul/')
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")
fp.set_preference("pdfjs.disabled", "true")
# disable Adobe Acrobat PDF preview plugin
fp.set_preference("plugin.scan.plid.all", "false")
fp.set_preference("plugin.scan.Acrobat", "99.0")
browser = webdriver.Firefox(firefox_profile=fp)
# Get the login web page
web_url = 'http://technical.traders.com/sub/sublogin2.asp'
browser.get(web_url)
# SImulate the authentication
user_name = browser.find_element_by_css_selector('#SubID > input[type="text"]')
user_name.send_keys("[email protected]")
password = browser.find_element_by_css_selector('#SubName > input[type="text"]')
password.send_keys("LastName")
time.sleep(2)
submit = browser.find_element_by_css_selector('#SubButton > input[type="submit"]')
submit.click()
time.sleep(2)
# Open the PDF for downloading
url = 'http://technical.traders.com/archive/articlefinal.asp?file=\V26\C07\\131INTR.pdf'
browser.get(url)
time.sleep(10)
# How to simulate the Clicking to Save/Download the PDF here?
我的代碼附加到我的代碼後#如何...但在我運行該程序時出現錯誤:TypeError:參數應該是類似字節的對象或ASCII字符串,而不是'NoneType'。我在下面再次發佈我的代碼。你能看看嗎? – thomas2013ch
嘗試在第二部分之前添加一個等待。我認爲這是因爲你試圖在onload函數完成之前獲取變量的內容 – TheChetan
嗨TheChetan,我設置了一個更長的暫停,並且確實下載了PDF。非常感謝! – thomas2013ch