这是关于如何从Google专利下载~1000个文件的this上一个问题的后续问题。
我想遍历文件名列表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))
然而,浏览器打开,但没有任何反应(没有点击文件)。有什么想法吗?
答案 0 :(得分:1)
您需要将文件名传递给XPath表达式:
filename = driver.find_element_by_xpath('//a[contains(text(), "{filename}")]'.format(filename=f))
尽管如此,更简单的定位技术是"by partial link text":
for f in fname:
filename = driver.find_element_by_partial_link_text(f)
filename.click()