2

Im working on getting the data from tripadvisor but most of the first ones are relative date and the rest are normal MM/DD/YYYY, but with closer inspection I see that relative date has this

<span class="ratingDate relativeDate" title="20 June 2015">Reviewed 4 weeks ago
</span>

I am using this Xpath to get the data

response.xpath('//div[@class="col2of2"]//span[@class="ratingDate relativeDat
e" or @class="ratingDate"]/text()').extract()

My question is How do I add the @title so that I can get the title which has the normal date format.

I tried

response.xpath('//div[@class="col2of2"]//span[@class="ratingDate relativeDat
e"/@title or @class="ratingDate"]/text()').extract()

response.xpath('//div[@class="col2of2"]//span[@class="ratingDate relativeDat
e" or @class="ratingDate"]/@title/text()').extract()
7
  • Also Forgot to mention I cannot have 2 seperate Xpaths because it is hard to format it in the pipline which prints to CSV Commented Jul 20, 2015 at 7:14
  • Why not? It is really easy to set the item's field to one of those XPath results. In this case the solution is transparent for your pipeline. Commented Jul 20, 2015 at 7:15
  • I just rialised that I can set it to the same field until the relative runs out and then the second one takes over. Thus letting me two Xpaths. But I still cant figure out how to call the title attribute Commented Jul 20, 2015 at 7:19
  • Figured it out, I was calling text while I shouldent have. response.xpath('//div[@class="col2of2"]//span[@class="ratingDate relativeDat e"]/@title').extract() Commented Jul 20, 2015 at 7:20
  • nevermind it need the text() which the title does not have Commented Jul 20, 2015 at 7:40

1 Answer 1

7

Figured it out in the spider you have to do a conditional statement that will dynamically check whether that xpath contains values or not.

Here's my rendition.

item['date'] = sel.xpath('//*[@class="ratingDate relativeDate"]/@title').extract()
item['date'] += sel.xpath('//div[@class="col2of2"]//span[@class="ratingDate"]/text()').extract()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.