数据提取--JSON

什么是数据提取?

  简单的来说,数据提取就是从响应中获取我们想要的数据的过程

非结构化的数据:html等 结构化数据:json,xml等
处理方法:正则表达式、xpath 处理方法:转化为python数据类型

由于把json数据转化为python内建数据类型很简单,所以爬虫中,如果我们能够找到返回json数据的URL,就会尽量使用这种URL

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写。同时也方便了机器进行解析和生成。

适用于进行数据交互的场景,比如网站前台与后台之间的数据交互。

那么问题来了:哪里能找到返回json的url呢?

1、使用chrome切换到手机页面

2、抓包手机app的软件

具有 read() 或者 write() 方法的对象就是类文件对象 f = open(“a.txt”,”r”) f就是类文件对象

url = "https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items?start=0&count=18&loc_id=108288"
html_str = parse_url(url)

# json.loads把json字符串转化为python类型
ret1 = json.loads(html_str)
# pprint(ret1)
# print(type(ret1))

# json.dumps能够把python类型转化为json字符串
with open("douban.json","w",encoding="utf-8") as f:
    f.write(json.dumps(ret1,ensure_ascii=False,indent=4))
    # f.write(str(ret1))

# with open("douban.json","r",encoding="utf-8") as f:
#     ret2 = f.read()
#     ret3 = json.loads(ret2)
#     print(ret3)
#     print(type(ret3))


# 使用json。load提取类文件对象中的数据
with open("douban.json","r",encoding="utf-8") as f:
    ret4 = json.load(f)
    print(ret4)
    print(type(ret4))

#json.dump能够把python类型放入类文件对象中
with open("douban1.json","w",encoding="utf-8") as f:
    json.dump(ret1,f,ensure_ascii=False,indent=2)

Json在数据交换中起到了一个载体的作用,承载着相互传递的数据

原文地址:https://www.cnblogs.com/wsg-python/p/10116080.html