zoukankan      html  css  js  c++  java
  • 一个豆瓣API的使用——拒绝思维定式

        好久没写博客了,最近一直在用豆瓣API爬数据,不知道以前的是什么样,毕竟刚开始用没多久,就用最新的V2版本,以前的不更新了,可以参照https://developers.douban.com/wiki/?title=api_v2,但是在用某些API的时候会出一些问题,需要自己琢磨琢磨,因为网上关于API爬数据的资料并不多,今天就聊一些豆瓣同城V2的一个接口的调用——获取活动列表。

        根据豆瓣同城V2提供的接口,获取活动列表:GET https://api.douban.com/v2/event/list,返回值是eventlist,其格式可以自己查询,我们试一下:

    try:    
        html = urllib2.urlopen(r'https://api.douban.com/v2/event/list')                           
    except Exception as ex:
        print ex
    hjson = json.loads(html.read())
    eventlist(hjson)

        这是主要代码,eventlist是定义的一个函数,输出活动列表,运行一下,出错,400bad request,还是不知道什么原因,从网页上跳一下https://api.douban.com/v2/event/list,得到一个文件,用浏览器打开,显示信息为

    {"msg":"invalid_parameter","code":1016,"request":"GET /v2/event/list"}

    ,参数错误,我的天哪,什么鬼,不是按照API说的做了吗,怎么出错了?难道获取列表的不能用?试一下获取城市列表:

    try:    
        html = urllib2.urlopen(r'https://api.douban.com/v2/loc/list')                
    except Exception as ex:
        print ex
    
    hjson = json.loads(html.read())
    city_print(hjson)

    city_print为定义的输出城市列表的函数,运行一下,完美输出,兄弟,蒙了吧。到底什么原因呢,百度一下,毫无进展,要不Google一下(呵呵),还得靠自己。从源头找找思路,打开豆瓣,进去同城子模块,发现左上角显示你的所在地,脑子闪过一个念头:会不会是获取的时候要指定地名?马上试试,但是怎么指定地名呢,想想前几天爬豆瓣电影top250的时候有个start参数,这样是不是也管用,管用的话用哪个词呢。在看获取城市列表的接口,是loc/list,那指定地名也用这个试试,说干就干:

    try:    
        html = urllib2.urlopen(r'https://api.douban.com/v2/event/list?loc=qingdao')
                                                          
    except Exception as ex:
        print ex
    hjson = json.loads(html.read())
    eventlist(hjson)

    因为我在青岛,就把地名指定为青岛,运行一下,简单看一些数据:

    -----------------------
    owner_name: 良友书坊文化机构
    owner_uid: liangyoubooks
    owner_id: 121533
    id: 26343442
    begin_time: 2016-03-06 10:00:00
    end_time: 2016-05-04 22:00:00
    address: 青岛 市南区 香港中路沿线 澳门路117号海信广场B1层 良友书坊•有度空间
    ----------------------
    owner_name: 我爱户外
    owner_uid: 128609
    owner_id: 128609
    id: 26248720
    begin_time: 2016-02-21 08:30:00
    end_time: 2016-05-15 16:00:00
    address: 青岛 崂山区 沙子口广场
    ----------------------
    owner_name: 摄影师贾葭
    owner_uid: LifephotoJia
    owner_id: 103740178
    id: 25637123
    begin_time: 2016-04-17 08:00:00
    end_time: 2016-07-15 17:00:00
    address: 青岛 市南区 湛山/太平角 适合拍摄的任意地点
    ----------------------

    完美!遇到问题自己解决的感觉就一个字——爽!完整代码也贴出来吧,写的有点拙劣,大神勿看。

    #---coding:utf-8---
    """
    Date: 2016-4-17
    Language: Python2.7.6
    by seven_clear
    """
    
    import json
    import urllib2
    import string
    
    def eventlist(json):
        '输出活动列表'
        print 'count:',json['count']
        print 'total:',json['total']
        print '-----------------------'
        for event in json['events']:
            print 'owner_name:',event['owner']['name']
            print 'owner_uid:',event['owner']['uid']
            print 'owner_id:',event['owner']['id']
            print 'id:',event['id']
            #print 'content:',event['content']
            print 'begin_time:',event['begin_time']
            print 'end_time:',event['end_time']
            print 'address:',event['address']
            print '----------------------'
    
    def city_print(json):
        '输出城市列表'
        print 'count:',json['count']
        print 'total:',json['total']
        print '----------------------------'
        for city in json['locs']:
            print 'parent:',city['parent']
            print 'id:',city['id']
            print 'name:',city['name']
            print 'uid:',city['uid']
            print '----------------------------'
    
    
    try:    
        html = urllib2.urlopen(r'https://api.douban.com/v2/event/list?loc=qingdao')
                               #https://api.douban.com/v2/loc/list')
                               
    except Exception as ex:
        print ex
    #print html.read()
    
    hjson = json.loads(html.read())
    eventlist(hjson)
    #city_print(hjson)
    
    for key in hjson:#测试json格式
        print key
  • 相关阅读:
    Bzoj3998 [TJOI2015]弦论
    Bzoj3669 [Noi2014]魔法森林
    Bzoj2157 旅游
    Bzoj3282 Tree
    POJ1743 Musical Theme
    CodeVs1515 跳
    Bzoj1013 [JSOI2008]球形空间产生器sphere
    Bzoj1007 [HNOI2008]水平可见直线
    洛谷P3120 [USACO15FEB]Cow Hopscotch
    洛谷P3112 [USACO14DEC]后卫马克Guard Mark
  • 原文地址:https://www.cnblogs.com/littleseven/p/5402886.html
Copyright © 2011-2022 走看看