V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yiqianbai
V2EX  ›  Python

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

  •  
  •   yiqianbai · 2017-08-15 16:00:44 +08:00 · 1692 次点击
    这是一个创建于 2448 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2435 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 16:01 · PVG 00:01 · LAX 09:01 · JFK 12:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.