Home > Mobile >  How can I select all paragraph elements inside nested divs?
How can I select all paragraph elements inside nested divs?

Time:02-12

I have a reviews that I want to scrape (https://www.consumeraffairs.com/insurance/bluecross_fl.html). The issue that I find is that some reviews aren't stored inside the same structure. In the page you have to press "read full review" and this access another part of the structure that contains the remaining of the review.

The structure for simple reviews is like this:

<div>
   <p>
     review
   </p>
</div>

The other reviews structure is like this:

<div>
   <p>
     review
   </p>
   <div>
       <p>
         remaining review
      </p>
   </div>
</div>

I have tried something like this but still I'm just getting the first part of the reviews and not the remaining.

response.css('itemprop="reviews"] > div:nth-child(3) > p *::text').extract()

CodePudding user response:

If you use the general child selector you should be able to target all of the p's in divs. Despite the structure.

div > p {
  background-color: #ffd110;
}
<div>
  <p>
    review
  </p>
  <div>
    <p>
      remaining review
    </p>
  </div>
</div>

<div>
  <p>
    review
  </p>
</div>

Edit ~ From requested in the comments, you can concentrate which p gets selected using p:nth-child(2) See below.

div > p:nth-child(2) {
  background-color: #ffd110;
}
<div>
  <p>
    review
  </p>
  <div>
    <p>
      remaining review
    </p>
    <p>review 2</p>
  </div>
</div>

  • Related