簡體   English   中英

在Python中使用Selenium循環下載文件

[英]Loop through downloading files using selenium in Python

這是一個后續問題關於如何從谷歌專利下載〜1000個文件前面的問題。

我想遍歷文件名列表fname = ["ipg150106.zip", "ipg150113.zip"]並模擬單擊並將這些文件保存到我的計算機。 以下示例對我有用,並下載一個文件:

from selenium import webdriver 
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

# Define parameters
savepath = 'D:\\' # set the desired path here for the files


# Download the files from Google Patents
profile = FirefoxProfile ()
profile.set_preference("browser.download.panel.shown", False) 

profile.set_preference("browser.download.folderList", 2) # 2 means specify custom location
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", savepath) # choose folder to download to
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",'application/octet-stream')

driver = webdriver.Firefox(firefox_profile=profile)

url = 'https://www.google.com/googlebooks/uspto-patents-grants-text.html#2015'
driver.get(url)

filename = driver.find_element_by_xpath('//a[contains(text(), "ipg150106.zip")]')
filename.click()

我試圖用一個列表和這樣的循環替換它:

fname = ["ipg150106.zip", "ipg150113.zip"]

for f in fname:
    filename = driver.find_element_by_xpath('//a[contains(text(), f)]')
    filename.click()
    print('Finished loop for: {}.'.format(f))

但是,瀏覽器將打開,但是沒有任何反應(不單擊文件)。 有任何想法嗎?

您需要將文件名傳遞到XPath表達式中:

filename = driver.find_element_by_xpath('//a[contains(text(), "{filename}")]'.format(filename=f))

不過,這里更簡單的定位技術是“通過部分鏈接文字”

for f in fname:
    filename = driver.find_element_by_partial_link_text(f)
    filename.click()

暫無
暫無

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

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