我正在使用scrapy
从here获取某个数据。正如我的建议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
后面的输出:
{
"9102-DBL-sprung slat base": {
"id": "9102",
"name": "Imperial Bedstead",
"url": "/p/Imperial_Bedstead.htm",
"description": "Double - Sprung Slat Base",
"unit_price": 429.99,
"unit_sale_price": 429.99,
"currency": "GBP",
"sku_code": "BENT:1320B-Beech",
"category": "Bed Frames",
"stock": 100
},
"9102-KS-sprung slat base": {
"id": "9102",
"name": "Imperial Bedstead",
"url": "/p/Imperial_Bedstead.htm",
"description": "Kingsize - Sprung Slat Base",
"unit_price": 439.98996,
"unit_sale_price": 439.98996,
"currency": "GBP",
"sku_code": "BENT:1326B-Beech",
"category": "Bed Frames",
"stock": 100
}
}
现在,我想将此json
类似的结构转换为python dict
。我试过了,但它返回unicode
类型。
j_data = json.loads(data)
那么,我如何在Python 2.7中获得Array / Dict?具有讽刺意味的是,使用loads
时dict
方法返回scrapy shell
类型。
答案 0 :(得分:1)
试试这个:
#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))