比较喜欢玩梦幻西游,最近准备买号,自己看梦幻藏宝阁呢 又没空,就想着用 scrapy 抓点符合要求的数据回来,但是,当抓到用户关键信息的时候,网易返回给我的是类似下面的一段字符串,我想着给他转成一个字典或者 python 内部能识别的数据格式,但是我发现我真的不知道怎么转化。我目前的方案之后一个,那就是正则匹配,但是我感觉正则匹配是不是蠢了点。有没有大神帮我解决下这个问题的。
这儿是返回的字符串:(我已经做好了换行,记住是一个字符串,不是 python 的 list、tuple、dict)
'([
"farm_level":"3",
"usernum":10836654,
"iMaxExpt3":20,
"iBeastSki4":17,
"rent":88332,
"iCGTotalAmount":30,
"xianyu":12,
"iHp_Max":2711,
"more_attr":(["attrs":({(["lv":0,"idx":1,]),(["lv":0,"idx":3,]),(["lv":0,"idx":9,]),(["lv":0,"idx":12,]),(["lv":0,"idx":8,]),(["lv":0,"idx":6,]),(["lv":0,"idx":11,]),(["lv":0,"idx":7,]),(["lv":0,"idx":5,]),(["lv":0,"idx":4,]),(["lv":0,"idx":2,]),(["lv":0,"idx":10,]),(["lv":129,"idx":13,]),(["lv":444,"idx":14,]),}),]),
"AllRider":([7:(["mattrib":"","ExtraGrow":0,"exgrow":12904,"iGrade":125,"iType":508,"all_skills":([]),]),5:(["mattrib":"","ExtraGrow":0,"exgrow":13106,"iGrade":125,"iType":503,"all_skills":([]),]),4:(["mattrib":"体质","ExtraGrow":0,"exgrow":13112,"iGrade":125,"iType":502,"all_skills":([611:1,]),]),6:(["mattrib":"魔力","ExtraGrow":100,"exgrow":23075,"iGrade":125,"iType":508,"all_skills":([600:2,601:3,]),]),2:(["mattrib":"力量","ExtraGrow":90,"exgrow":22297,"iGrade":125,"iType":503,"all_skills":([600:2,601:3,]),]),]),
"iLearnCash":8213515,
"iBadness":0,
"ExAvt":([11:(["order":7,"cName":"锦绣幻梦","iType":12823,]),9:(["order":11,"cName":"藤蔓蔓延","iType":14694,]),6:(["order":4,"cName":"落星织","iType":12750,]),2:(["order":5,"cName":"蓝色妖姬","iType":14404,]),1:(["order":10,"cName":"星光熠熠","iType":21513,]),3:(["order":9,"cName":"锦绣幻梦","iType":12817,]),10:(["order":8,"cName":"两心相悦","iType":14654,]),8:(["order":6,"cName":"星光熠熠","iType":21514,]),7:(["order":1,"cName":"萌草发卡","iType":13696,]),39:(["order":2,"cName":"狼牙棒","iType":11169,]),40:(["order":12,"cName":"怀旧巨魔王炫卡","iType":19287,]),4:(["order":3,"cName":"落星织","iType":40013,]),]),
"iDef_All":410,
"iSewski":472,
"iSumAmount":9,
"iSpe_All":119,
"iMaxExpt4":20,
"sword_score":1000,
"AchPointTotal":4520,
"iGoodness":7230,
"addPoint":28,
"iSumAmountEx":1,
"cName":"水儋澹兮生烟",
"iSaving":0,
"rent_level":5,
"iBeastSki3":17,
......
)]'
1
rabbbit 2018-10-18 17:38:05 +08:00
import json
json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') |
2
gz911122 2018-10-18 17:52:42 +08:00
这不就是这个正常的 json 吗
|
3
KomeijiSatori 2018-10-18 17:56:20 +08:00
|
4
cassidyhere 2018-10-18 18:15:47 +08:00
[]换成{}, {}换成[],删除(),就是 dict 了
|
5
frostming 2018-10-18 18:34:00 +08:00
()删掉
{}和[]互换 然后 json.loads |
6
zongren 2018-10-19 08:36:03 +08:00
此贴终结
|
9
moxiaowei OP @cassidyhere 关键就是如何替换
|
11
chenstack 2018-10-19 11:23:14 +08:00
楼上说的差不多了,只是替换后仍不是标准 json,有的键为数字,且有逗号结尾,不能直接 json.loads。还有一个是你给的字符串开头是([,结尾是)],并不匹配,如果是成对的]),可以这样:
eval(text.replace('([', '{').replace('])', '}').replace('({', '[').replace('})', ']')) 如果想把()替换删掉,{}和[]互换,那替换时得加个临时的替换符号 |
12
moxiaowei OP 哎 果然只有死方法,用正则先替换,谢谢大家 解析出来了
|