【发布时间】:2017-08-10 04:48:04
【问题描述】:
我正在使用 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'}}
它不是另存为格式“vănbảnhợpnhất02/ vbhn-bgdđtnăm2017Hướngdẫnquyếtđịnh152/ 2007 /qđ-ttgvềhọcbổngchínhsáchđốivớihīcinh,sinhviênhọctạicơsởgiáodụcthuộ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"。
请帮我创建这个 unicode json 字符串。
谢谢
【问题讨论】:
标签: python json unicode lxml python-unicode