【问题标题】:Scrapy uses wrong encoding, adds extra html tags to JSON from webpageScrapy 使用错误的编码,从网页向 JSON 添加额外的 html 标签
【发布时间】:2017-06-22 16:12:27
【问题描述】:

所以我想用 Scrapy 来获取 Puerto Rico board game data

数据如下所示

  { "data": {
    ...
    "label":"<div class=\"iblock\">\u262f &ge; 75%<\/div>"
    ...
   }

但是,Scrapy 中的 response.text 对象使用了不同的编码并添加了一些额外的 html 标签:

<html><head></head><body>
 {"data": { 
   ... 
 "label": "<div class="\&quot;iblock\&quot;">\u262f ≥ 75%&lt;\/div&gt;"
  ...
  }
</div></body></html>

因此,当我尝试将 json 解析为 python 对象时:

responseJSON = json.loads(response.xpath("/html/body/text").extract_first())

我收到以下错误:

ValueError: end is out of bounds

我怎样才能让 Scrapy 返回一个没有额外 html 标签的正确编码的响应?

【问题讨论】:

  • 显示如何获取此字符串。也许问题出在不同的地方。您使用 selenium 或类似工具吗?

标签: python html json web-scraping scrapy


【解决方案1】:
json.loads(response.text) 

这是json响应,不需要使用xpath

在scrapy shell中测试

【讨论】:

  • 我试过了,但它给了我 ValueError("No JSON object could be decoded")。
  • 想通了。我的一个中间件把它搞砸了。现在你的答案有效
猜你喜欢
  • 2016-12-19
  • 1970-01-01
  • 2023-02-13
  • 2017-06-08
  • 2011-08-29
  • 1970-01-01
  • 2013-01-02
  • 2014-01-21
  • 2012-11-03
相关资源
最近更新 更多