如何分配unicode json字符串

时间:2017-03-18 17:36:51

标签: python json unicode lxml python-unicode

我正在使用lxml并尝试将解析后的数据放到json字符串中。但我的数据是unicode字符串,它会自动转换。

这是我的代码:

from lxml import html,etree
import pprint
import requests
url="http://thuvienphapluat.vn"
page = requests.get(url)
tree=html.fromstring(page.content)
vbplm=tree.xpath('//div[@id="VBPLMOI"]//div[@class="left-col"]')
rlst={}
# print etree.tostring(tree.find('./a'),pretty_print=True)
import re
for vb in vbplm:
    id = re.sub(r"\n*\s",'',vb.xpath('.//*[@class="number"]/text()')[0])
    rlst[id]={}
    tmp=vb.xpath('.//a')

    for tpm_part in tmp:
        rlst[id][
                (tpm_part.xpath('.//text()'))[0].encode(encoding='utf-8')
            ]=((tpm_part.get("href")))
        print (tpm_part.xpath('.//text()'))[0].encode(encoding='utf-8')
        print "<<<<<<<<"
        break
    break
pprint.pprint(rlst)

这是我的结果:

Văn bản hợp nhất 02/VBHN-BGDĐT năm 2017 hướng dẫn Quyết định 152/2007/QĐ-TTg về học bổng chính sách đối với học sinh, sinh viên học tại cơ sở giáo dục thuộc hệ thống giáo dục quốc dân do Bộ Giáo dục và Đào tạo ban hành
<<<<<<<<

    {'1': {'V\xc4\x83n b\xe1\xba\xa3n h\xe1\xbb\xa3p nh\xe1\xba\xa5t 02/VBHN-BGD\xc4\x90T n\xc4\x83m 2017 h\xc6\xb0\xe1\xbb\x9bng d\xe1\xba\xabn Quy\xe1\xba\xbft \xc4\x91\xe1\xbb\x8bnh 152/2007/Q\xc4\x90-TTg v\xe1\xbb\x81 h\xe1\xbb\x8dc b\xe1\xbb\x95ng ch\xc3\xadnh s\xc3\xa1ch \xc4\x91\xe1\xbb\x91i v\xe1\xbb\x9bi h\xe1\xbb\x8dc sinh, sinh vi\xc3\xaan h\xe1\xbb\x8dc t\xe1\xba\xa1i c\xc6\xa1 s\xe1\xbb\x9f gi\xc3\xa1o d\xe1\xbb\xa5c thu\xe1\xbb\x99c h\xe1\xbb\x87 th\xe1\xbb\x91ng gi\xc3\xa1o d\xe1\xbb\xa5c qu\xe1\xbb\x91c d\xc3\xa2n do B\xe1\xbb\x99 Gi\xc3\xa1o d\xe1\xbb\xa5c v\xc3\xa0 \xc4\x90\xc3\xa0o t\xe1\xba\xa1o ban h\xc3\xa0nh':
 'http://thuvienphapluat.vn/van-ban/Giao-duc/Van-ban-hop-nhat-02-VBHN-BGDDT-huong-dan-152-2007-QD-TTg-hoc-bong-chinh-sach-hoc-sinh-sinh-vien-342726.aspx'}}

它不保存为格式“VAN禁止跳山一02 / VBHN-BGDĐTNAM 2017 HUONG丹Quyếtđịnh二千○七分之一百五十二/ QDJ-TTg号VE HOC奉chínhSACHđốiVOI HOC双曲正弦,双曲正弦维恩HOC TAI CO所以GIAO DUCthuộc hệthốnggiáodụcquốcdéndoBộGiáodụcvĐàotạonbanhành“。

请帮我创建这个unicode json字符串。

由于

1 个答案:

答案 0 :(得分:0)

你只有一个Python词典。您需要使用json模块生成JSON字符串。

import json
print(json.dumps(rlst))