无法在scray中将返回的JSON数据转换为Dict

2024-05-29 03:06:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用scrapyhere获取一段特定的数据。正如建议的here,我在脚本中使用了以下代码:

pattern = re.compile(r"qubit_product_list = (.*?);", re.M)
        script = hxs.select("//script[contains(., 'qubit_product_list')]/text()").extract()[0]
        data = pattern.search(script).group(1)

        j_data = json.loads(data)
        self.log('After calling LOAD Begins')
        self.log(j_data) #It is not printing ANYTHING!!!!
        self.log('After calling LOAD Ends')

        self.log('\n---------------------------------\n')

它从变量data输出以下内容:

^{pr2}$

现在,我想将这个类似于json的结构转换成pythondict。我尝试了以下操作,但它返回unicode类型。在

j_data = json.loads(data)

那么,如何在python2.7中获得Array/Dict呢?具有讽刺意味的是,loads方法在使用scrapy shell时返回了dict类型。在


Tags: selfrelogjsondataherescriptproduct
1条回答
网友
1楼 · 发布于 2024-05-29 03:06:57

试试这个:

#typecasting the JSON to string for json.loads to work
data = str(data)
#returning type dict from json
j_data = json.loads(data)
#typecasting the dict to string before writing to log
self.log(str(j_data)) 

相关问题 更多 >

    热门问题