我正在尝试从URL https://play.google.com/store/apps/details?id=com.teslacoilsw.launcher&hl=zh_CN中获取数据,并获取以下数据
<meta content="3.99" itemprop="price">
我使用以下在Python中实现的代码进行提取,但失败了。
browser = webdriver.Firefox() # Get local session of firefox
browser.get(sampleURL) # Load page
assert "Google Play" in browser.title
priceValue = browser.find_element_by_xpath("//div[@itemprop='price']")#
print priceValue.text
但是它说它找不到价值价格的xpath。知道为什么吗?
编辑
priceValue = browser.find_element_by_xpath("//meta[@itemprop='price']")
print priceValue.text
我得到空字符串
如果我查看页面源代码,例如在Chrome中view-source:https://play.google.com/store/apps/details?id=com.teslacoilsw.launcher&hl=en
。我也找不到<div>
具有attribute@itemprop
和value的元素price
。
因此,您的XPath是完全错误的。还browser.find_element_by_xpath()
返回一个元素,并且您要提取的属性值@content
。然后,您应该使用下一个:
priceValue = browser.find_element_by_xpath("//meta[@itemprop='price']")
print priceValue.get_attribute("content")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句