2015-06-21 53 views
1

這是對this上一個關於如何從Google專利中下載1000個文件的問題的後續問題。使用Python在Python中下載文件循環使用

我想遍歷一個文件名列表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)) 

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

回答

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()