Scrapy xpath删除文本<字符

时间:2015-11-03 13:58:59

标签: python xpath web-scraping scrapy parsel

我正在尝试从this页面获取产品信息。要获得描述(显示在页面底部),我使用xpath

response.xpath('//*[@itemprop="description"]/table//text()').extract()[3].strip()

这给了我描述:

u'Color: White, Size:Free Size, With the body: Braided, Buckle: Automatic Deduction, With the body width: section ('

而网站上的那个是

Color: White, Size:Free Size, With the body: Braided, Buckle: Automatic Deduction, With the body width: section (<2cm), Belt Length: 93cm
Product Type: Belts, Accessories

我已经确认即使在停用javascript后网站上的内容也会加载。我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

这仍然应该在没有任何 hack 的情况下处理,但你可以使用它:

from parsel import Selector
...

s = Selector(text=response.body_as_unicode(), type='xml')
s.xpath('//*[@itemprop="description"]/table//text()').extract()[3].strip()
# gives u'Color: White, Size:Free Size, With the body: Braided, Buckle: Automatic Deduction, With the body width: section (2cm), Belt Length: 93cm'

这里的问题是parsel(内部scrapy解析器)使用lxml.etree.HtmlParser(recover=True, encoding='utf8')删除了这种奇怪的字符以避免问题。