import json
from urllib import request
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'
}
# 豆瓣电影url
url = "https://movie.douban.com/j/new_search_subjects?sort=T&range=0,10&tags=&start=0"
# 发送请求
req = request.Request(url, headers=headers)
response = request.urlopen(req)
content = response.read().decode()
# json解析
result = json.loads(content)
# 获取电影数据
movie = result['data']
for n in movie:
title = n['title'] # 电影名称
url = n['url'] # url
# 文件存储
with open('douban.txt', 'a', encoding='utf-8') as fp:
s = f'{title},{url}\n'
fp.write(s)