Xpath和css_选择器无法提取动态内容。(使用Python和Selenium)

2024-09-21 01:12:55 发布

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

编辑:感谢电子邮件解决了我的问题。你使用了一些我不熟悉的代码,所以你给了我一些很好的学习材料

原始帖子:我在Python中使用Selenium获取教育统计数据。我一整天都在试图从以下网站中提取一个数字——长期缺勤率,该网站包含美国伊利诺伊州的信息:https://www.illinoisreportcard.com/School.aspx?schoolid=340491250130001该数字(在本例中为“10%”)位于一个带有类“解释”的div元素中

<p class="image" id="thumb6" data-type="partition">
  <svg class="canvas" width="256" height="220" viewBox="0 0 256 220">...</svg>==0
  <div class="explanation" style="position: absolute; width: 110px; text-align: center; top: 82px; left: 73px;">10%</div>
</p>

我尝试了以下所有方法和更多方法,包括显式等待,以选择包含此图的div元素,但都失败了,通常会导致NoTouchElementException:

driver.find_element_by_class_name('explanation')
driver.find_element_by_xpath("//div[@class='explanation']")
#Trying to reach parent element: 
driver.find_element_by_xpath("//p[@id='thumb6']")
driver.find_element_by_xpath(/html[1]/body[1]/div[1]/div[1]/a[7]/p[1]/svg[1]/g[1]/rect[1])

我相信,但不确定,这个问题可能与动态内容有关,但我不确定HTML代码实际上是否是动态的,因为我以前没有遇到过任何问题。有人能帮助我理解为什么我不能提取这个数字吗

谢谢。非常感谢您的帮助


Tags: 代码svgdivid元素by网站driver
2条回答

我认为你不需要硒来做这个。首先,建立一个URL列表。模式是:

https://rcc.isbe.net/api/reportcardservice/(en)/Domain(school)/Id(340491250130001)/(Profile)/(2019)/Table/(Xml)

其中Id(340491250130001)是每所学校的id。(2019)是感兴趣的年份。如果需要,可以指定年数范围(2016-2019)

对于列表中的每个url,您需要获取包含数据的ressource url。XPath:

//resourceUrl

您将得到如下结果:

https://sec.isbe.net/iircapi/tempData/XML/File1992993354.xml

对于每个xml文件,您将通过以下方式获得长期缺勤率:

//ChronicAbsenteeism

例如:

from lxml import html
import requests

data = requests.get('https://rcc.isbe.net/api/reportcardservice/(en)/Domain(school)/Id(340491250130001)/(Profile)/(2019)/Table/(Xml)')
root = html.fromstring(data.content)
xml=root.xpath('//resourceurl/text()')[0]

source = requests.get(xml)
tree = html.fromstring(source.content)
print(tree.xpath('//chronicabsenteeism/text()')[0])

输出:10

下面是解决这个问题的快速方法:

driver.find_element_by_xpath("//div[@class='explanation']").text() # This will fetch the innerHTML i.e. value of the div

相关问题 更多 >

    热门问题