Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I select all paragraph elements inside nested divs?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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()

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading