【问题标题】:Trying to download PDF's from website using HTML table in dataframe?尝试使用数据框中的 HTML 表格从网站下载 PDF?
【发布时间】:2020-04-15 14:59:09
【问题描述】:

我已经从https://www.asx.com.au/asx/statistics/prevBusDayAnns.do 抓取了 HTML 表格。我已将此表放入熊猫数据框中。我还为名为“Match”的数据框创建了另一列,如果“ASX Code”=“SPL”,它会显示 1。如果您查看网站,您会看到标题是 PDF 文件的标题可以下载。如果“匹配”列 = 1,我想下载文件。这可能吗?硒?

我的代码:

import pandas as pd
dfs = pd.read_html('https://www.asx.com.au/asx/statistics/prevBusDayAnns.do')
for df in dfs:
    df.loc[df['ASX Code'] == 'SPL', 'Match'] = "1"
    df.loc[df['ASX Code'] != 'SPL', 'Match'] = "0"
    print(df)

数据框:

    ASX Code                 Date  Price sens.                                           Headline Match
0        SPL  15/04/2020  7:25 PM          NaN  SPL7013 shows significant activity against cor...     1
1        LSH  15/04/2020  7:19 PM          NaN  Change of Director's Interest Notice  2  pages...     0
2        PSQ  15/04/2020  7:14 PM          NaN  PSQ Implements Dividend Reinvestment Plan  25 ...     0
3        TGN  15/04/2020  7:11 PM          NaN  March Quarterly Report and Appendix 5B  24  pa...     0
4        GRR  15/04/2020  6:49 PM          NaN  Change of Director's Interest Notice  3  pages...     0

【问题讨论】:

  • 为什么需要熊猫?如果你想根据ASX Code 下载 pdf,你只能使用 selenium 来完成
  • 我不需要它,我只是为了方便使用它。我该怎么做?

标签: python pandas selenium


【解决方案1】:

你需要做几件事。

您需要设置chrome option 自动下载pdf。

诱导WebdriverWait并等待element_to_be_clickable()

诱导WebdriverWait等待windows再切换到window再点击舔Agreed and proceed

点击该 pdf 文件将自动下载到您的默认下载文件夹。

代码

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

chromeOptions=webdriver.ChromeOptions()
prefs = {"plugins.always_open_pdf_externally": True}
chromeOptions.add_experimental_option("prefs",prefs)
driver=webdriver.Chrome(executable_path="path/to/chromedriver",chrome_options=chromeOptions)
driver.get("https://www.asx.com.au/asx/statistics/prevBusDayAnns.do")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//table//tr//td[text()='SPL']/following-sibling::td[3]/a"))).click()
WebDriverWait(driver,15).until(EC.number_of_windows_to_be(2))
driver.switch_to.window(driver.window_handles[-1])
WebDriverWait(driver,15).until(EC.element_to_be_clickable((By.XPATH,"//input[@value='Agree and proceed']"))).click()

浏览器快照:

【讨论】:

  • 无论如何我可以更改此行,以便我可以下载多个“ASX 代码”的文件 WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//table //tr//td[text()='SPL']/following-sibling::td[3]/a"))).click()
  • 是的,您可以。但是,您需要迭代 ASX 代码列表和逻辑会有点不同。我希望发布一个新的问题,提出新的要求。谢谢
  • 我发布了一个新问题,如果您能看一下,将不胜感激。 stackoverflow.com/q/61268278/12875715
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
  • 2016-08-10
  • 2020-12-29
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 2023-03-15
相关资源
最近更新 更多