【发布时间】:2015-10-09 05:31:27
【问题描述】:
我正在努力从 tripadvisor 获取数据,但大多数第一个是相对日期,其余的是正常的 MM/DD/YYYY,但仔细检查后我发现相对日期有这个
<span class="ratingDate relativeDate" title="20 June 2015">Reviewed 4 weeks ago
</span>
我正在使用这个 Xpath 来获取数据
response.xpath('//div[@class="col2of2"]//span[@class="ratingDate relativeDat
e" or @class="ratingDate"]/text()').extract()
我的问题是如何添加@title 以便获得具有正常日期格式的标题。
我试过了
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()
【问题讨论】:
-
另外忘了提我不能有 2 个单独的 Xpath,因为很难在打印到 CSV 的管道中格式化它
-
为什么不呢?将项目的字段设置为这些 XPath 结果之一真的很容易。在这种情况下,解决方案对您的管道是透明的。
-
我刚刚意识到我可以将它设置为相同的字段,直到相对用完,然后第二个接管。因此让我有两个 Xpath。但是我还是不知道怎么调用title属性
-
想通了,我本来应该有的时候打电话给短信。
response.xpath('//div[@class="col2of2"]//span[@class="ratingDate relativeDat e"]/@title').extract() -
没关系它需要标题没有的 text()
标签: python xpath web-scraping scrapy