How to get the number of <p> tags inside a div in scrapy?

I clean this link site .

The last tag <p>contains user_info, and this creates a problem for me, as I use -

''.join(response.xpath('//div[@class="entry-content"]/p[2]/text()').extract())

But it p[2]changes if the text above it is in good quantity. Let's say itp[5]

I think about it to calculate the number of tags <p>inside divand assign a number to myitem

How to deal with this problem?

+4
source share
2 answers

From what I understand, this is only the last paragraph in the contents of the entry - you can use last():

//div[@class="entry-content"]/p[last()]/text()

It works for me.

+3

p

len(response.xpath('//div[@class="entry-content"]/p'))
+2

Source: https://habr.com/ru/post/1609417/


All Articles