为了账号安全,请及时绑定邮箱和手机立即绑定

通过xpath查找多个td标签

通过xpath查找多个td标签

元芳怎么了 2024-01-03 15:47:37
如果满足两个条件,我想从该网站https://www.asx.com.au/asx/statistics/prevBusDayAnns.do下载 PDF 文件。第一个条件是“ASX 代码”必须与列表中的代码之一匹配。第二个条件是“标题”必须与“实质性持有量变化”相匹配。我当前的代码仅在“ASX Code”=“SPL”时通过 xpath 查找。我想要实现的目标的一个例子:data1 = ['SPL', 'WBC', 'AAA']WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//table//tr//td[text()={data1}]/following-sibling::td[3]/a"))).click()我的代码:chromeOptions=webdriver.ChromeOptions()prefs = {"plugins.always_open_pdf_externally": True}chromeOptions.add_experimental_option("prefs",prefs)driver=webdriver.Chrome(executable_path=r"C:\Users\Harrison Pollock\Downloads\Python\chromedriver_win32\chromedriver.exe",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()
查看完整描述

1 回答

?
慕容3067478

TA贡献1773条经验 获得超3个赞

在所在位置的网页上找不到包含 ASX 代码的数据集。但是,data1 = ['SPL', 'WBC', 'AAA']这里是如何按顺序下载多个 ASX 代码的示例。


数据集:data1 = ['SW1', 'AME', 'BGA','PPT','AMP']


将 的值存储href在列表中,然后迭代该列表并单击“同意”按钮下载 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

import time


chromeOptions=webdriver.ChromeOptions()

prefs = {"plugins.always_open_pdf_externally": True}

chromeOptions.add_experimental_option("prefs",prefs)

driver=webdriver.Chrome(executable_path=r"C:\Users\Harrison Pollock\Downloads\Python\chromedriver_win32\chromedriver.exe",chrome_options=chromeOptions)

driver.get("https://www.asx.com.au/asx/statistics/prevBusDayAnns.do")

data1 = ['SW1', 'AME', 'BGA','PPT','AMP']


pdfUrls=[]

for d in data1:

    try:

       pdfurl=driver.find_element_by_xpath("//table//tr//td[text()='{}']/following-sibling::td[3]/a[contains(.,'{}')]".format(d,"Change in substantial holding")).get_attribute("href")

       pdfUrls.append(pdfurl)

    except:

        print("No ASX code found with Headline Change in substantial holding : " + d)



for pdfurl in pdfUrls:

    driver.get(pdfurl)

    WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Agree and proceed']"))).click()

    time.sleep(10)  # pause to check download

    print("Downloaded pdf file")


查看完整回答
反对 回复 2024-01-03
  • 1 回答
  • 0 关注
  • 34 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信