Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用selenium循环下载文件_Python_Selenium_Selenium Webdriver_Web Scraping_Python 3.4 - Fatal编程技术网

在Python中使用selenium循环下载文件

在Python中使用selenium循环下载文件,python,selenium,selenium-webdriver,web-scraping,python-3.4,Python,Selenium,Selenium Webdriver,Web Scraping,Python 3.4,这是上一个关于如何从谷歌专利下载约1000个文件的问题的后续问题 我想遍历文件名列表fname=[“ipg150106.zip”,“ipg150113.zip”]并模拟单击这些文件并将其保存到我的计算机。以下示例适用于我并下载单个文件: from selenium import webdriver from selenium.webdriver.firefox.firefox_profile import FirefoxProfile # Define parameters savepath

这是上一个关于如何从谷歌专利下载约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()