gpt4 book ai didi

python - 在 python selenium 中从 HTML 表中提取链接文本

转载 作者:行者123 更新时间:2023-12-02 03:24:49 24 4
gpt4 key购买 nike

我是 Selenium 的新手。我想从网站的以下 HTML 代码中的 HTML 表中提取链接文本。

代码片段:

<div style="width:210px" id="calenderdiv">
<table id="calender" align="center" bgcolor="#ABABAB" width="90%" cellspacing="1" cellpadding="0" border="0">
<tbody>
<tr height="25" bgcolor="#DDDDDD" style="font-family:arial ;font-size:12;font-weight:bold; color: #006699">
<td align="center" width="14%">S</td>
<td align="center" width="14%">M</td>
<td align="center" width="14%">T</td>
<td align="center" width="14%">W</td>
<td align="center" width="14%">T</td>
<td align="center" width="14%">F</td>
<td align="center" width="14%">S</td>
</tr>
<tr height="25" bgcolor="#FFFFFF" style="font-family:arial ;font-size:12;font-weight:bold; color: #006699">
<td bgcolor="#EFEFEF" align="center"><a href="/2010/1/1/archivelist/year-2010,month-1,starttime-40179.cms"></a>&nbsp; </td>
<td align="center"><a href="/2010/1/1/archivelist/year-2010,month-1,starttime-40179.cms"></a>&nbsp; </td>
<td align="center"><a href="/2010/1/1/archivelist/year-2010,month-1,starttime-40179.cms"></a>&nbsp; </td>
<td align="center"><a href="/2010/1/1/archivelist/year-2010,month-1,starttime-40179.cms"></a>&nbsp; </td>
<td align="center"><a href="/2010/1/1/archivelist/year-2010,month-1,starttime-40179.cms"></a>&nbsp; </td>
<td align="center"><a href="/2010/1/1/archivelist/year-2010,month-1,starttime-40179.cms">1</a></td>
<td align="center"><a href="/2010/1/2/archivelist/year-2010,month-1,starttime-40180.cms">2</a></td>
</tr>
<tr height="25" bgcolor="#FFFFFF" style="font-family:arial ;font-size:12;font-weight:bold; color: #006699">
<td bgcolor="#EFEFEF" align="center"><a href="/2010/1/3/archivelist/year-2010,month-1,starttime-40181.cms">3</a></td>
<td align="center"><a href="/2010/1/4/archivelist/year-2010,month-1,starttime-40182.cms">4</a></td>
<td align="center"><a href="/2010/1/5/archivelist/year-2010,month-1,starttime-40183.cms">5</a></td>
<td align="center"><a href="/2010/1/6/archivelist/year-2010,month-1,starttime-40184.cms">6</a></td>
<td align="center"><a href="/2010/1/7/archivelist/year-2010,month-1,starttime-40185.cms">7</a></td>
<td align="center"><a href="/2010/1/8/archivelist/year-2010,month-1,starttime-40186.cms">8</a></td>
<td align="center"><a href="/2010/1/9/archivelist/year-2010,month-1,starttime-40187.cms">9</a></td>
</tr>
<tr height="25" bgcolor="#FFFFFF" style="font-family:arial ;font-size:12;font-weight:bold; color: #006699">
<td bgcolor="#EFEFEF" align="center"><a href="/2010/1/10/archivelist/year-2010,month-1,starttime-40188.cms">10</a></td>
<td align="center"><a href="/2010/1/11/archivelist/year-2010,month-1,starttime-40189.cms">11</a></td>
<td align="center"><a href="/2010/1/12/archivelist/year-2010,month-1,starttime-40190.cms">12</a></td>
<td align="center"><a href="/2010/1/13/archivelist/year-2010,month-1,starttime-40191.cms">13</a></td>
<td align="center"><a href="/2010/1/14/archivelist/year-2010,month-1,starttime-40192.cms">14</a></td>
<td align="center"><a href="/2010/1/15/archivelist/year-2010,month-1,starttime-40193.cms">15</a></td>
<td align="center"><a href="/2010/1/16/archivelist/year-2010,month-1,starttime-40194.cms">16</a></td>
</tr>
<tr height="25" bgcolor="#FFFFFF" style="font-family:arial ;font-size:12;font-weight:bold; color: #006699">
<td bgcolor="#EFEFEF" align="center"><a href="/2010/1/17/archivelist/year-2010,month-1,starttime-40195.cms">17</a></td>
<td align="center"><a href="/2010/1/18/archivelist/year-2010,month-1,starttime-40196.cms">18</a></td>
<td align="center"><a href="/2010/1/19/archivelist/year-2010,month-1,starttime-40197.cms">19</a></td>
<td align="center"><a href="/2010/1/20/archivelist/year-2010,month-1,starttime-40198.cms">20</a></td>
<td align="center"><a href="/2010/1/21/archivelist/year-2010,month-1,starttime-40199.cms">21</a></td>
<td align="center"><a href="/2010/1/22/archivelist/year-2010,month-1,starttime-40200.cms">22</a></td>
<td align="center"><a href="/2010/1/23/archivelist/year-2010,month-1,starttime-40201.cms">23</a></td>
</tr>
<tr height="25" bgcolor="#FFFFFF" style="font-family:arial ;font-size:12;font-weight:bold; color: #006699">
<td bgcolor="#EFEFEF" align="center"><a href="/2010/1/24/archivelist/year-2010,month-1,starttime-40202.cms">24</a></td>
<td align="center"><a href="/2010/1/25/archivelist/year-2010,month-1,starttime-40203.cms">25</a></td>
<td align="center"><a href="/2010/1/26/archivelist/year-2010,month-1,starttime-40204.cms">26</a></td>
<td align="center"><a href="/2010/1/27/archivelist/year-2010,month-1,starttime-40205.cms">27</a></td>
<td align="center"><a href="/2010/1/28/archivelist/year-2010,month-1,starttime-40206.cms">28</a></td>
<td align="center"><a href="/2010/1/29/archivelist/year-2010,month-1,starttime-40207.cms">29</a></td>
<td align="center"><a href="/2010/1/30/archivelist/year-2010,month-1,starttime-40208.cms">30</a></td>
</tr>
<tr height="25" bgcolor="#FFFFFF" style="font-family:arial ;font-size:12;font-weight:bold; color: #006699">
<td bgcolor="#EFEFEF" align="center"><a href="/2010/1/31/archivelist/year-2010,month-1,starttime-40209.cms">31</a></td>
<td>&nbsp; </td>
<td>&nbsp; </td>
<td>&nbsp; </td>
<td>&nbsp; </td>
<td>&nbsp; </td>
<td>&nbsp; </td>
</tr>
</tbody>
</table>

对于上面的代码片段,我在 selenium 中编写了以下代码:

from selenium import webdriver
from selenium.webdriver.common.by import By

option = webdriver.ChromeOptions()
option.add_argument("--incognito")
option.add_argument("--start-maximized")

chrome_path = r"C:\Users\singh\Downloads\chromedriver_win32\chromedriver.exe"
browser = webdriver.Chrome(chrome_path, options=option)
browser.get("https://timesofindia.indiatimes.com/archive/year-2010,month-1.cms")
browser.implicitly_wait(10)

num = []
numbers = browser.find_elements(By.XPATH, "//table[@class = 'calender']/tbody/tr/td/a[@href]")

for n in numbers:
number = n.text
num.append(number)

预期输出:

num = ['S', 'M', 'T', 'W', 'T', 'F', 'S', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31']

程序将 numnumbers 列表返回为空列表。

我知道代码中输入的 XPath 中有些内容不正确。但我无法弄清楚错误是什么。

操作系统:Windows 10 x64
Python IDE:Anaconda Spyder
Python 版本:3.6

最佳答案

有两个问题

1.calender是id,而不是class。

2.要获取href,您需要使用get_attribute,而不是文本

numbers = browser.find_elements(By.XPATH, '//table[@id="calender"]//a')

for n in numbers:
number = n.get_attribute('href')
num.append(number)

关于python - 在 python selenium 中从 HTML 表中提取链接文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53690565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com