Python爬取Json格式数据并读写(改中文编码)

Python Json文件读写

经过测试发现,req.json()返回的类型是dict(字典类型)
如果有兴趣的话,可以再加一个正则表达式来筛选出url的关键名字,用关键名来做文件名。
import requests
import json
def writeinjson(url, headers):
    req = requests.get(url, headers=headers)
    with open("./name.json", "w") as f:
        json.dump(req.json(), f, ensure_ascii=False)
    return req.json()

def readJson(path):
    with open("./name.json", "r") as f:
        return json.load(f)

你可能感兴趣的:(Python,计算机基础,爬虫)