0
import scrapy 
import json 
class GettingtonDSpider(scrapy.Spider): 
    name = "gettington_d" 
    allowed_domains = ["gettington.com"] 
    start_urls = ['https://api.gettington.com/v1/products?showMPP=false&rows=24&q=Keyword:south%20shore%20furniture&productfilter=null&callback=searchCallback'] 
    def parse(self, response): 
    jsonresp = json.dumps(response.body) 
    jsonresp= json.loads(jsonresp) 

我已经尝试过很多方法,但我失败了:无法为Unicode转换为JSON在scrapy

  • response.text
  • 编码( 'UTF-8')
  • response_body_as_unicode

以上都无效。如何解决错误?

+1

dict你遇到任何错误?任何具体问题? “以上都没有奏效。”不是很有帮助。 – user312016

+0

是的,我得到了[json对象无法解码]。 –

+0

print(response.body)的输出是什么? – user312016

回答

0

,你必须从response.body首先删除不必要的信息,这是不是JSON序列化:

import re 

    ... 
    json_string = re.search(r'searchCallback\((.*)\)', response.body).group(1); 
    jsonresp = json.loads(json_string) 

现在你有一个在jsonresp

+0

太棒了,它工作正常。非常感谢,朋友。 –

+1

您还可以获取'https://api.gettington.com/v1/products?showMPP = false&rows = 24&q =关键字:south%20shore%20furniture&productfilter = null&format = json'(即删除'&callback = searchCallback',用于JSON格式) –