python - 通过 selenium 从 python 中的 html 表的第二列的每第二行获取 pdf 文件

标签 python html selenium web-scraping selenium-chromedriver

我是网络抓取的新手。我需要从下面 HTML 表格代码的第 2 列的每第 2 行获取 pdf 文件:

<table class="tablebg" width="100%">
    <tbody>
        <tr>
            <th colspan="4" align="left">Nov 09, 2017</th></tr>
                <tr>
                    <td style="word-wrap:break-word;width:450;">
                        <a class="link2" href="FS_Notification.aspx?Id=11162&amp;fn=5&amp;Mode=0">Risk Management and Inter-Bank Dealings – Simplified Hedging Facility</a>
                    </td>
                    <td nowrap="" colspan="3">
                        <a target="_blank" href="http://rbidocs.rbi.org.in/rdocs/notification/PDFs/APD118ED4C6E75FAC43A0BA5A738C21F8A8A7.PDF"><img src="../Images/pdf.gif" border="0" align="bsmiddle"></a>
                        97 kb
                    </td>
                </tr>

我试过下面的代码,但它没有获取第二行的第二列:

from selenium import webdriver

chrome_path = r"C:/chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
browser = driver.get("https://rbi.org.in/")

driver.find_element_by_xpath("""//*[@id="FEMA"]/a""").click()
driver.find_element_by_xpath("""//*[@id="FEMANotifications"]""").click()
result = driver.find_elements_by_xpath("//table//tr")

for rows in result:
    second_row = result.__getitem__(2)
    second_col = second_row.find_elements_by_partial_link_text("http://")
    print(second_col)

请提出任何帮助?

最佳答案

要打印 html 表格的第 2 列的第 2 行,您可以使用以下代码行:

print(driver.find_elements_by_xpath("//table[@class='tablebg']//tr//td/a[contains(@href,'http://rbidocs.rbi.org.in/rdocs/notification/PDFs')]").get_attribute('href'))

关于python - 通过 selenium 从 python 中的 html 表的第二列的每第二行获取 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47324062/

相关文章:

python - 使用 Python Selenium 保存上传的文件时遇到问题

selenium - 如何基于 Protractor 的相对值构建 xpath/css-selector

python - 将 16 位整数转换为 32 位 float

javascript - 修改:after pseudo CSS element from JQuery的宽度

python - 如何在 pandas 数据框和 SQL 表之间进行内连接?

html - 自定义 css 不能覆盖 bootstrap css

javascript - 折叠 DataTable 中的表头破坏了我的表格格式

java - 如何最大化 Selenium 屏

python - 在 TensorFlow 2.0 中,如何将 TFRecord 数据提供给 keras 模型?

python - BeautifulSoup 是具有转义字符的 HTML 转义字符串