request豆瓣电影(get请求+json数据+灵活采集)

import requests
import json

if __name__=='__main__':
    #1
    # url = 'https://movie.douban.com/j/chart/top_list?type=24&interval_id=100%3A90&action=&start=20&limit=20'
    url = 'https://movie.douban.com/j/chart/top_list' #方便灵活采集

    #阿贾克斯请求,查看到header最下面的申请信息
    param={
        'type': '24',
        'interval_id': '100:90',
        'action':'',
        'start': '0',#从库中的第几部电影去取
        'limit': '20',#一次取出的个数
    }

    headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
    }
    #2
    response = requests.get(url=url,params=param,headers=headers)

    #3
    res = response.json()

    #4
    filename='douban.json'
    f=open(filename,'w',encoding='utf-8')
    json.dump(res,f,ensure_ascii=False)

    print('爬取完成!')