【问题标题】:Extracting star rating using Selenium使用 Selenium 提取星级
【发布时间】:2015-06-03 10:31:05
【问题描述】:

我正在尝试使用Selenium从评论中提取星级,html标签如下:

 <p class="inlineRating starRating"><span class="current-rating" style="width: 80%">
        Current Rating: 4</span></p>

使用selenium,我做到了

rating = driver.find_element_by_css_selector('#ctnStars > div.catRatings.firstEl.clearfix > p.inlineRating.starRating > span')    
rating = rating.text

我总是得到的输出是:

当前评分:0

我也试过xpath的方法,scrapy在shell页面上遇到困难,我在python编码。请帮忙。

页面网址:http://www.webmd.com/drugs/drugreview-19924-cyclophosphamide+intravenous.aspx?drugid=19924&drugname=cyclophosphamide+intravenous&sortby=3

【问题讨论】:

  • 您从哪个页面提取这些值?请也发布网址。
  • 如果查看Overall User Ratings中的评分源代码,您会看到当前评分为0。如果查看其他部分的评分,则该值那里很好。
  • 如果是xpath,你可以试试这个substring-after(//span[@class="current-rating" ]/text() ,"Current Rating: ")

标签: python html css selenium xpath


【解决方案1】:

由于有多个评论,driver.find_element_by_css_selector() 调用返回的评论可能与您想象的不同。您需要先找到评论者,然后才能获得评论。类似的东西应该可以工作(如果我得到了正确的 Python 循环):

user_posts = driver.find_elements_by_css_selector('div.userPost')
for each user_post in user_posts
   effectiveness_rating = user_post.find_element_by_css_selector('#ctnStars > div.catRatings.firstEl.clearfix > p.inlineRating.starRating > span')

【讨论】:

    【解决方案2】:

    为特定审阅者找到有效星级评级的 xpath 将是:

    //p[@class='reviewerInfo' and contains(text(),'Reviewer: Sandy')]/following-sibling::div//div[contains(@class,'catRatings firstEl clearfix')]//span[@class='current-rating']/text()
    

    你可以对其他类别做同样的事情......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 2021-07-20
      • 1970-01-01
      • 2021-10-13
      • 2016-09-27
      • 2013-07-13
      • 1970-01-01
      相关资源
      最近更新 更多