Scrapy - Scraping selected div

Viewed 111

the data i want to scrape is layed out like this:

<div class="ads__unit">
<div>2001</div>
<div>10 000</div>
<div>200 000</div>
</div>

There are multiple items with this div class. Im trying to scrape the first div within the add_unit div for every item. Is there a way i can select the first div?

3 Answers

XPATH Selector:

//div[@class="ads__unit"]/div[1]/text()

CSS Selector:

div.ads__unit div:first-child

CSS Selector (Scrapy):

div.ads__unit div:first-child::text

Ive found that you can use

nth-child(n)

to select the differnt div in the same class

You also can use get() method to get first of element of list in any selector:

response.xpath('//div[@class="ads__unit"]/div/text()').get()

or index nth the element of list

response.xpath('//div[@class="ads__unit"]/div/text()').getall()[n]
Related