Answer the question
In order to leave comments, you need to log in
Scrapy: how after response.xpath to get one element and not a list?
Hello fellow toasters.
I am already writing a script on Scrapy and a question arose that I could not solve myself: suppose there is such a code -
def parse_item(self, response):
sel = Selector(response)
image = sel.xpath('//*[@id="listing-images"]/div/a/@href').extract()[0]
name = sel.xpath('//*[@id="main"]/div/h1/text()').extract()[0]
Answer the question
In order to leave comments, you need to log in
first_or_none = lambda x: x[0] if x else None
Here's a little lambda function that returns the first element of a sequence, or None.
How to return one element after xpath?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question