2014-05-02 51 views
2

这里的时候,问题与编码是的网站,我想解析:web-site in russianScrapy ::倾销JSON文件

这里是提取信息的代码,我需要:

# -*- coding: utf-8 -*- 
from scrapy.spider import Spider 
from scrapy.selector import Selector 
from flats.items import FlatsItem 

class DmozSpider(Spider): 
name = "dmoz" 
start_urls = ['http://rieltor.ua/flats-sale/?ncrnd=6510'] 

def parse(self, response): 
    sel=Selector(response) 
    flats=sel.xpath('//*[@id="content"]') 
    flats_stored_info=[] 
    flat_item=FlatsItem() 
    for flat in flats: 
     flat_item['square']=[s.encode("utf-8") for s in sel.xpath('//div/strong[@class="param"][1]/text()').extract()] 
     flat_item['rooms_floor_floors']=[s.encode("utf-8") for s in sel.xpath('//div/strong[@class="param"][2]/text()').extract()] 
     flat_item['address']=[s.encode("utf-8") for s in flat.xpath('//*[@id="content"]//h2/a/text()').extract()] 
     flat_item['price']=[s.encode("utf-8") for s in flat.xpath('//div[@class="cost"]/strong/text()').extract()] 
     flat_item['subway']=[s.encode("utf-8") for s in flat.xpath('//span[@class="flag flag-location"]/a/text()').extract()] 
     flats_stored_info.append(flat_item) 
    return flats_stored_info 

如何我转储到JSON文件

scrapy crawl dmoz -o items.json -t json 

问题是,当我更换上面的代码在控制台打印,即这样提取的信息:

flat_item['square']=sel.xpath('//div/strong[@class="param"][1]/text()').extract() 
    for bla in flat_item['square']: 
     print bla 

该脚本正确显示俄语信息。

但是,当我使用转储使用脚本的第一个版本的sraped信息(与编码成UTF-8),将其写入JSON文件是这样的:

[{"square": ["2-\u043a\u043e\u043c\u043d., 16 \u044d\u0442\u0430\u0436 16-\u044d\u0442. \u0434\u043e\u043c", "1-\u043a\u043e\u043c\u043d., 

我怎样才能将信息转储成俄文的json文件?感谢您的建议。

+0

你能证明你是如何倾倒JSON? Spasibo。 – alecxe

+0

请参阅编辑的帖子。 –

回答

5

它被正确编码,它只是json库默认转义非ascii字符。

您可以加载数据,并使用它(从你的例子复制数据):

>>> import json 
>>> print json.loads('"2-\u043a\u043e\u043c\u043d., 16 \u044d\u0442\u0430\u0436 16-\u044d\u0442. \u0434\u043e\u043c"') 
2-комн., 16 этаж 16-эт. дом