user13368804
user13368804

Reputation:

Find star rating

On this page I am trying to find a star rating.

In scrapy shell:

len(response.css('#info-panel .selected'))

len(response.css('#info-panel').xpath('//*[has-class("star selected")]'))

But nothing works out.

html

<div class="search-item-info" doctordetail=".1.0.0.0.1.3">
  <span doctordetail=".1.0.0.0.1.3.0">
  <span class="star-rating" title="3.92" doctordetail=".1.0.0.0.1.3.0.0">
  <span class="stars" doctordetail=".1.0.0.0.1.3.0.0.0">
  <span class="star" doctordetail=".1.0.0.0.1.3.0.0.0.$5"></span>
  <span class="star selected" doctordetail=".1.0.0.0.1.3.0.0.0.$4"></span>
  <span class="star selected" doctordetail=".1.0.0.0.1.3.0.0.0.$3"></span>
  <span class="star selected" doctordetail=".1.0.0.0.1.3.0.0.0.$2"></span>
  <span class="star selected" doctordetail=".1.0.0.0.1.3.0.0.0.$1"></span></span>
  </span>
  <div class="star-rating-count" doctordetail=".1.0.0.0.1.3.0.1">
    <span doctordetail=".1.0.0.0.1.3.0.1.0">
    <span itemprop="ratingCount" doctordetail=".1.0.0.0.1.3.0.1.0.0">28</span></span>
    <span doctordetail=".1.0.0.0.1.3.0.1.1"> reviews</span></div>
  </span>
</div>

Upvotes: 0

Views: 292

Answers (2)

E.Wiest
E.Wiest

Reputation: 5915

It's possible you get a 403 error (leading to an empty response) since the website uses Cloudfare protection. Then, you should probably use https://github.com/Anorov/cloudflare-scrape to bypass it.

If that's not the case, you can use one of the following XPath expression to get your star rating :

count(//h1/following::span[@class="stars"][1]//span[@class="star selected"])

Output : 4

More precise :

substring-before(substring-after(//script[contains(@type,"json")],'"ratingValue": "'),'"')

Output : 3.9

Upvotes: 0

Umair Ayub
Umair Ayub

Reputation: 21341

Try this

response.css(".star-rating::attr(title)").get()

Upvotes: 1

Related Questions