一個豆瓣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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM