Python 爬豆瓣 API 评分
打开终端:source activate course_py35
conda install jupyter 下载过的忽略此步
jupyter notebook
选择 New-Python3
import urllib.request control+enter
import urllib.request as urlrequest
url_visit='https://api.douban.com/v2/movie/26387939'
crawl_content=urlrequest.urlopen(url_visit).read()
print(crawl_content.decode('utf8')) control+enter:英文编译内容
import urllib.request as urlrequest
url_visit='https://api.douban.com/v2/movie/26387939'
crawl_content=urlrequest.urlopen(url_visit).read()
print(crawl_content.decode('unicode-escape')) control+enter:中文内容
选择:insert-insert cell below
import json
json_content=json.loads(crawl_content.decode('utf8'))control+enter
print(json_content['rating']['average']) control+enter:9.2
id=26387939
rank=json_content['rating']['average']
with open("douban_movie_rank.txt","w")as outputfile:
outputfile.write("{} {} ".format(id,rank)) control+enter:文件 douban_movie_rank.txt 已存储,打开文件内容为 26387939 9.2
import urllib.request as urlrequest
import json
id_list=[11803087,20451290,26387939]
with open("douban_movie_rank.txt","w")as outputfile:
for id in id_list:
url_visit='https://api.douban.com/v2/movie/{}'.format(id)
crawl_content=urlrequest.urlopen(url_visit).read()
json_content=json.loads(crawl_content.decode('utf8'))
#print(crawl_content.decode('unicode-escape'))
#print(json_content['rating']['average'])
rank=json_content['rating']['average']
outputfile.write("{} {}\n".format(id,rank)) control+enter:指定三个电影的评分存储,内容为;
11803087 7.4
20451290 4.8
26387939 9.2
以下链接不用看,我方便上课跳转的:
https://class.pkbigdata.com/#/classDetail/classIntroduce/1?slxydc=V2EXsjfxsrm