import urllib.request as request
import json #完整代码
url = 'https://api.douban.com/v2/movie/us_box' #开放的api接口地址
crawl_content=request.urlopen(url).read()
top20 = json.loads(crawl_content.decode('utf-8'))['subjects']#json解析我们想要获取的内容
# print(top20)
for movie in top20:
# print(movie['subject']['id']) #相对应电影的id在嵌套的json里面
# print()
url='https://api.douban.com/v2/movie/'+movie['subject']['id'] #相对应电影的地址
print(url)
mobieContent = request.urlopen(url).read()
print(json.loads(mobieContent.decode('utf-8'))['title']+':'+json.loads(mobieContent.decode('utf-8'))['rating']['average'])
# 获取对应电影的名称和分数
查看豆瓣开放的API:
豆瓣开发者文档: