如何在python中使用selenium将锚定标记及其超链接复制到excel中

2024-05-03 07:25:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将锚定标记及其值和超链接复制到excel中。 我已经编写了将数据传输到excel的代码。别担心。 我所需要知道的就是如何将超链接复制到excel中。我已经尝试了很多,我只能复制锚标记的值,但不能复制链接

我想获取名为“ATP酶活动”的超链接

enter image description here

并复制到excel表格中。当我手动将其复制到excel中时,我能够复制“ATP酶活动”及其链接,该链接变成蓝色并带有下划线

enter image description here

但同样的,我无法使用selenium代码

超链接“ATP酶活动”的检查元素

enter image description here

我试过使用

driver.find_elements_by_xpath('//ul[@class="noNumbering biological_process"]/li/a')[0].text

但它只给出了“ATP酶活性”的值,而没有给出它的联系。我试过了

driver.find_elements_by_xpath('//ul[@class="noNumberingbiological_process"]/li/a')[0].get_attribute('href')

但这会将链接https://www.ebi.ac.uk/QuickGO/term/GO:0042026作为输出

我需要一个超链接,其显示为“ATP酶活性”,其链接https://www.ebi.ac.uk/QuickGO/term/GO:0042026在网站上显示相同


Tags: 代码标记by链接driverlielementsfind
1条回答
网友
1楼 · 发布于 2024-05-03 07:25:58

基本上,这是标签中的href。Selenium提供了一个功能,我们可以使用get_attribute获取标记属性,下面是该功能的示例代码:

link = driver.find_elements_by_xpath('//ul[@class="noNumbering biological_process"]/li/a')
print(link.get_attribute('href'))
print(link.text)

相关问题 更多 >