使用Selenium Python从页面源获取元标记

2024-05-29 05:15:26 发布

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

我正试图从URL https://play.google.com/store/apps/details?id=com.teslacoilsw.launcher&hl=en获取数据并获取下面的数据

   <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

我得到空字符串


Tags: texthttpsbrowsercomurlbyelementfind
1条回答
网友
1楼 · 发布于 2024-05-29 05:15:26

如果我查看页面源代码,例如Chrome view-source:https://play.google.com/store/apps/details?id=com.teslacoilsw.launcher&hl=en。我也找不到具有属性@itemprop和值price<div>元素。

所以您的XPath是完全错误的。同时browser.find_element_by_xpath()返回一个元素,您需要提取@content的属性值。然后您应该使用next:

priceValue = browser.find_element_by_xpath("//meta[@itemprop='price']")
print priceValue.get_attribute("content")

相关问题 更多 >

    热门问题