Python json解析

公子盛 2018-07-09 07:47:51
爬虫爬取今日头条的时候,解析出一些图片链接是以json格式存在的,但是这些json是特殊处理过的,如下:
所有特殊字符都用反斜杠作了转义,请问该如何处理才能获取正确的信息?
{\"count\":4,\"sub_images\":[{\"url\":\"http:\\/\\/p1.pstatp.com\\/origin\\/pgc-image\\/1524670089467caf2fff4c4\",\"width\":1280,\"url_list\":[{\"url\":\"http:\\/\\/p1.pstatp.com\\/origin\\/pgc-image\\/1524670089467caf2fff4c4\"},{\"url\":\"http:\\/\\/pb3.pstatp.com\\/origin\\/pgc-image\\/1524670089467caf2fff4c4\"},{\"url\":\"http:\\/\\/pb9.pstatp.com\\/origin\\/pgc-image\\/1524670089467caf2fff4c4\"}],\"uri\":\"origin\\/pgc-image\\/1524670089467caf2fff4c4\",\"height\":1007},{\"url\":\"http:\\/\\/p9.pstatp.com\\/origin\\/pgc-image\\/1524670089355a07d28f775\",\"width\":1280,\"url_list\":[{\"url\":\"http:\\/\\/p9.pstatp.com\\/origin\\/pgc-image\\/1524670089355a07d28f775\"},{\"url\":\"http:\\/\\/pb1.pstatp.com\\/origin\\/pgc-image\\/1524670089355a07d28f775\"},{\"url\":\"http:\\/\\/pb3.pstatp.com\\/origin\\/pgc-image\\/1524670089355a07d28f775\"}],\"uri\":\"origin\\/pgc-image\\/1524670089355a07d28f775\",\"height\":900},{\"url\":\"http:\\/\\/p3.pstatp.com\\/origin\\/pgc-image\\/1524670089412445c95fe1e\",\"width\":1280,\"url_list\":[{\"url\":\"http:\\/\\/p3.pstatp.com\\/origin\\/pgc-image\\/1524670089412445c95fe1e\"},{\"url\":\"http:\\/\\/pb9.pstatp.com\\/origin\\/pgc-image\\/1524670089412445c95fe1e\"},{\"url\":\"http:\\/\\/pb1.pstatp.com\\/origin\\/pgc-image\\/1524670089412445c95fe1e\"}],\"uri\":\"origin\\/pgc-image\\/1524670089412445c95fe1e\",\"height\":1022},{\"url\":\"http:\\/\\/p9.pstatp.com\\/origin\\/pgc-image\\/152467008944401ff089475\",\"width\":1280,\"url_list\":[{\"url\":\"http:\\/\\/p9.pstatp.com\\/origin\\/pgc-image\\/152467008944401ff089475\"},{\"url\":\"http:\\/\\/pb1.pstatp.com\\/origin\\/pgc-image\\/152467008944401ff089475\"},{\"url\":\"http:\\/\\/pb3.pstatp.com\\/origin\\/pgc-image\\/152467008944401ff089475\"}],\"uri\":\"origin\\/pgc-image\\/152467008944401ff089475\",\"height\":1151}],\"max_img_width\":1280,\"labels\":[\"\\u51b7\\u996e\",\"\\u7f8e\\u5973\",\"\\u5386\\u53f2\"],\"sub_abstracts\":[\"\\u5357\\u4eac\\u8def\\u5df2\\u6709100\\u591a\\u5e74\\u7684\\u5386\\u53f2\\uff0c\\u5b83\\u7684\\u524d\\u8eab\\u662f\\\"\\u6d3e\\u514b\\u5f04\\\"\\uff0c1865\\u5e74\\u6b63\\u5f0f\\u547d\\u540d\\u4e3a\\u5357\\u4eac\\u8def\\u3002\\u56fe\\u4e3a\\u4e24\\u5916\\u56fd\\u7f8e\\u5973\\u559d\\u51b7\\u996e\\u3002\\uff08\\u56fe\\u7247\\u6765\\u81ea\\u4e1c\\u65b9IC\\uff09\",\"\\u56fe\\u4e3a\\u5357\\u4eac\\u8def\\u6b65\\u884c\\u8857\\u4e0a\\u7684\\u5916\\u56fd\\u53cb\\u4eba\\u4e00\\u8def\\u4e0e\\u51b7\\u996e\\u76f8\\u4f34\\u3002\\uff08\\u56fe\\u7247\\u6765\\u81ea\\u4e1c\\u65b9IC\\uff09\",\"\\u4e0e\\u51b7\\u996e\\u4e3a\\u4f34\\u3002\\uff08\\u56fe\\u7247\\u6765\\u81ea\\u4e1c\\u65b9IC\\uff09\",\"\\u5929\\u6c14\\u786e\\u5b9e\\u6709\\u70b9\\u70ed\\u3002\\uff08\\u56fe\\u7247\\u6765\\u81ea\\u4e1c\\u65b9IC\\uff09\"],\"sub_titles\":[\"\\u5357\\u4eac\\u8def\\u6b65\\u884c\\u8857\\u8857\\u62cd\",\"\\u5357\\u4eac\\u8def\\u6b65\\u884c\\u8857\\u8857\\u62cd\",\"\\u5357\\u4eac\\u8def\\u6b65\\u884c\\u8857\\u8857\\u62cd\",\"\\u5357\\u4eac\\u8def\\u6b65\\u884c\\u8857\\u8857\\u62cd\"]}


...全文
161 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
公子盛 2018-07-10
  • 打赏
  • 举报
回复
下文转自: http://www.bkjia.com/Pythonjc/1121974.html
python转换已转义的字符串

有时我们可能会获取得以下这样的字符串:

Python代码 收藏代码
>>> a = '{\\"name\\":\\"michael\\"}'
>>> print a
{\"name\":\"michael\"}


那么该如何将其转换为一个字典呢?

可以用以下的方法:

Python代码 收藏代码
>>> type(json.loads('“' + a + '”'))
<type 'unicode'>
>>> type(json.loads(json.loads('“' + a + '”')))
<type 'dict'>
第一次json.loads是将里面的\"这样的字符串转为"(只有一个双引号),第二次再将其转为一个字典,记得不要漏掉前面先加双引号。
公子盛 2018-07-10
  • 打赏
  • 举报
回复
以解决。
网上找到一份文档:http://www.bkjia.com/Pythonjc/1121974.html
上面说像我这种情况,需要在外面套一个双引号,然后json.loads(json.load("json串"))
我仔细检查了网页源码和我的脚本,发现在我json串外有一对双引号,我以为不是json的内容,在我通过re获取的时候被我丢弃了。
以下是修改后的代码,只粘贴修改的函数,第5、9行是我修改的地方

def get_image_link(image_html):
soup = BeautifulSoup(image_html, 'lxml')
title = soup.select("title")[0].text
print(title)
image_patt = re.compile('gallery: JSON.parse\((.*?)\)', re.S)
# image_list = re.search(image_patt, image_html).group(1).replace("\\", "")
image_list = re.search(image_patt, image_html).group(1)
# print(image_list)
image_json =[color= json.loads(json.loads(image_list))
print(image_json)
sub_images = image_json['sub_images']
images = [item.get('url') for item in sub_images]
print(images)
公子盛 2018-07-10
  • 打赏
  • 举报
回复
引用 7 楼 oyljerry 的回复:
先看看这个字符串是不是有什么特殊的地方,是不是还有其他格式

爬下来的原始数据是在所有特殊字符前都加了反斜杠的特殊json串。
传递给变量的时候,将反斜杠识别成了其中的字符,例: 原始\" ,传递给变量之后变成了 \\"
json.loads()转换的时候,因为不是标准的json格式,报错了:json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2
这种特殊的字符串,有什么办法转换吗?


手工将上述字符串赋值给变量,然后json.loads()转换是成功的
上面报错的提示JSONDecodeError与在赋值时a=r"json串",然后json.loads()报错是一致的。
oyljerry 2018-07-10
  • 打赏
  • 举报
回复
先看看这个字符串是不是有什么特殊的地方,是不是还有其他格式
公子盛 2018-07-10
  • 打赏
  • 举报
回复
给自己顶个贴,大家帮忙看一下
公子盛 2018-07-09
  • 打赏
  • 举报
回复
引用 4 楼 ambit_tsai 的回复:
[quote=引用 3 楼 shenghaomail的回复:][quote=引用 1 楼 ambit_tsai 的回复:]
直接用json库,反序列化一下

# encoding: utf-8
import json
data = json.loads(json_str)
print(data)


谢谢答复,单独拿这条字符串转换json串是没问题的。
我放到爬虫脚本里执行就报错
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)


[/quote]
报错部分的代码贴出来看看[/quote]


以下代码是爬取今日头条上一组图片的链接,我找到的图片链接信息在doc下
# 先获取网页内容
def get_image_group(html_url):
try:
res = requests.get(html_url)
if res.status_code == 200:
return res.text
return None
except RequestException:
print("请求详情页出错")
return None


# 找到要抓取的图片链接所在位置,并解析
def get_image_link(image_html):
soup = BeautifulSoup(image_html, 'lxml')
title = soup.select("title")[0].text
print(title)
image_patt = re.compile('gallery: JSON.parse\(\"(.*?)\"\)', re.S)
# image_list = re.search(image_patt, image_html).group(1).replace("\\", "")
image_list = re.search(image_patt, image_html).group(1)
print(image_list)
image_json = json.loads(image_list)
sub_images = image_json['sub_images']
images = [item.get('url') for item in sub_images]
print(images)


def main():

image_info = get_image_group("https://www.toutiao.com/a6548409069143589389/")
get_image_link(image_info)

ambit_tsai-微信 2018-07-09
  • 打赏
  • 举报
回复
引用 3 楼 shenghaomail的回复:
[quote=引用 1 楼 ambit_tsai 的回复:]
直接用json库,反序列化一下

# encoding: utf-8
import json
data = json.loads(json_str)
print(data)


谢谢答复,单独拿这条字符串转换json串是没问题的。
我放到爬虫脚本里执行就报错
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)


[/quote] 报错部分的代码贴出来看看
公子盛 2018-07-09
  • 打赏
  • 举报
回复
引用 1 楼 ambit_tsai 的回复:
直接用json库,反序列化一下

# encoding: utf-8
import json
data = json.loads(json_str)
print(data)


谢谢答复,单独拿这条字符串转换json串是没问题的。
我放到爬虫脚本里执行就报错
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)


ambit_tsai-微信 2018-07-09
  • 打赏
  • 举报
回复
引用 1 楼 ambit_tsai 的回复:
直接用json库,反序列化一下

# encoding: utf-8
import json
data = json.loads(json_str)
print(data)


程序自己能识别转义符的,放心
ambit_tsai-微信 2018-07-09
  • 打赏
  • 举报
回复
直接用json库,反序列化一下

# encoding: utf-8
import json
data = json.loads(json_str)
print(data)

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧