一个豆瓣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
最新文章
- JavaScript的基准测试-不服跑个分?
- 汽车遥控钥匙HCS101/HCS200/HCS201/HCS300芯片解密
- 【Linux】unzip命令,记一次遇到的问题
- [ActionScript 3.0] AS3.0 本机鼠标指针
- BZOJ 1218: [HNOI2003]激光炸弹 前缀DP
- webpack echarts配置实例
- PDF合并
- HTML5之一HTML5简介
- 联系我们_站内信息_站内资讯_网上定制衬衫|衬衫定制|衬衫定做-ChenShanLe衬衫乐
- iOS-NSAttributedString自定义文字变色
- mbed 初试(LinkNode 板卡)(2016-04-21)
- C#利用Emit反射实现AOP,以及平台化框架封装思路
- Git 解决同步 No value for key branch.master.merge found in
- C# 关于操作datatable的列名和删除某一列的数据
- django集成celery之callback方式link_error和on_failure
- jenkins 解决构建成功后进程消失的问题
- .Net 特性 attribute 学习 ----自定义特性
- Monkey脚本API
- (转载)SPARKR,对RDD操作的介绍
- UVA11584-Partitioning by Palindromes(动态规划基础)