数据分析入门学习第 2 篇| Python 爬豆瓣 API 评分

2017-08-15 16:00:44 +08:00
 yiqianbai
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
1794 次点击
所在节点    Python
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/383118

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX