编辑:感谢EDIT解决了我的问题。你用了一些我不熟悉的代码,所以你给了我一些好的学习材料。
原始文章:我在中使用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元素,但都失败了,通常导致NoSuchElementException:
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代码实际上是否是动态的,因为我以前没有遇到过任何问题。有人能帮我弄明白为什么我不能提取这个数字吗?
谢谢。任何帮助都很感激。
发布于 2020-05-10 03:13:45
我不认为你需要硒。首先,构建一个urls列表。其模式是:
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
发布于 2020-05-10 03:38:37
以下是快速解决的方法:
driver.find_element_by_xpath("//div[@class='explanation']").text() # This will fetch the innerHTML i.e. value of the divhttps://stackoverflow.com/questions/61706095
复制相似问题