【问题标题】:Scrapy xpath removing text after < characterScrapy xpath 删除 < 字符后的文本
【发布时间】:2015-11-03 13:58:59
【问题描述】:

我正在尝试从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 后也会加载。我在这里错过了什么?

【问题讨论】:

  • 好像是因为&lt;符号而被截断了,连BeautifulSoup都截掉了&lt;之后的文字……很奇怪
  • 这是一个parsel 错误,我会在存储库here 上检查它

标签: python xpath web-scraping scrapy parsel


【解决方案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'),它会删除这种奇怪的字符以避免出现问题。

【讨论】:

    猜你喜欢
    • 2020-03-17
    • 2013-04-10
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多