![](/img/trans.png)
[英]How to pass items in a list into xpath to download files from a web site?
[英]How can we loop through items, and download text files from a web site in bulk?
我試圖弄清楚如何遍歷 ListBox 中的項目並下載和批量下載文本文件。
這是我正在查看的鏈接。
https://cdr.ffiec.gov/public/PWS/DownloadBulkData.aspx
我想 select 這個產品。
“電話報告——資產負債表、損益表、逾期——四個時期”
然后遍歷 2020-2012 年,並將這些文件批量下載到我的本地硬盤上。
我在瀏覽器中按 F11 並輕松找到按鈕和“DatesDropDownList”,但我沒有看到任何鏈接到可用於進行所有下載的文本文件的 URL。 你需要Selenium嗎?
作為在列表中選擇內容和單擊按鈕的替代方法,是否有某種 web 服務可以使此過程更容易?
我會使用 selenium。 它是在 Python 中實現您在 web 瀏覽器中手動執行的直接路徑。
這是您提供的示例。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# using Google Chrome, can use the browser of your choice
driver = webdriver.Chrome('PATH/TO/chromedriver.exe')
url = 'https://cdr.ffiec.gov/public/PWS/DownloadBulkData.aspx'
driver.get(url)
等待可用產品加載,然后 select 值。
path = "//select[@id='ListBox1']"
products = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.XPATH, path)
)
)
select = 'Call Reports -- Balance Sheet, Income Statement, Past Due -- Four Periods'
driver.find_element_by_xpath(path+"/option[text()='"+select+"']").click()
等待數年加載。 獲取年份列表。 Select 其中之一作為示例。
path = "//select[@id='DatesDropDownList']"
dropdown = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.XPATH, path)
)
)
dates = driver.find_elements_by_xpath(path+'/option')
# an example, you can loop through dates
driver.find_element_by_xpath(path+"/option[text()='"+dates[10].text+"']").click()
從這里您可以解壓縮文件,將它們加載到 Pandas DataFrame 中,然后存儲在 Excel 文件、數據庫等中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.