提取span标签内的信息

问题描述 投票:1回答:1

我试图在“span”标签之间提取PMC ID。

为此,我使用了find by xpath,但我遇到了以下错误:

selenium.common.exceptions.NoSuchElementException:Message: Unable to locate element: /div/main/div/details/div/div[2]/details/summary/span[5]

以下是链接:

https://www.ncbi.nlm.nih.gov/pmc/utils/idconv/v1.0/?tool=my_tool&[email protected]&ids=9811893

以下是我的代码:

driver = webdriver.Firefox(executable_path='geckodriver.exe')
driver.implicitly_wait(10)  # this lets webdriver wait 10 seconds for the website to load
driver.get("https://www.ncbi.nlm.nih.gov/pmc/utils/idconv/v1.0/?tool=my_tool&[email protected]&ids=9811893")
pmc= driver.find_element_by_xpath('/div/main/div/details/div/div[2]/details/summary/span[5]')
pmc.get_text()

输出应该是:

PMC24938
python python-3.x selenium-webdriver web-scraping beautifulsoup
1个回答
1
投票

您可以使用css属性选择器,然后使用get_attribute来获取属性值

from selenium import webdriver
driver = webdriver.Firefox(executable_path='geckodriver.exe')
driver.get("https://www.ncbi.nlm.nih.gov/pmc/utils/idconv/v1.0/?tool=my_tool&[email protected]&ids=9811893")
pmc = driver.find_element_by_css_selector('[pmcid]')
print(pmc.get_attribute('pmcid'))

结果:

enter image description here


虽然你不需要selenium这个网站。使用更快的requestsbs4

import requests
from bs4 import BeautifulSoup as bs
r = requests.get('https://www.ncbi.nlm.nih.gov/pmc/utils/idconv/v1.0/?tool=my_tool&[email protected]&ids=9811893')
soup = bs(r.content, 'lxml')
pmc = soup.select_one('[pmcid]')['pmcid']
print(pmc)
© www.soinside.com 2019 - 2024. All rights reserved.