Python获取豆瓣用户读过的书

好久好久不写博客了,最近也光顾着玩了,前几天跟导师联系了一下毕设的事,要再爬点东西,想了想,就拿豆瓣图书练练手,爬一下某个用户读过的书(当然也可以好多用户或者其他信息),由于豆瓣提供了API,所以首先想到用API,图书API地址:https://developers.douban.com/wiki/?title=book_v2,看了看,好像没有获取用户读过的书之类的,擦亮眼再看看,有一个获取某用户所有图书收藏信息,那这个到底是不是呢,试一下就知道了。

那个接口里有个参数status,表示收藏状态(想读:wish 在读:reading 读过:read 默认为所有状态),我们将status参数填read就是读过的书,先测一下:

#---coding:utf-8---
import urllib2
import json
import string
import time
import math

def book_p(book_json):
    print ‘count‘,book_json[‘count‘]
    print ‘start:‘,book_json[‘start‘]
    print ‘total‘,book_json[‘total‘]
    print ‘---------------------------‘
    for co in book_json[‘collections‘]:
        print ‘title:‘,co[‘book‘][‘title‘]
        print ‘isbn10:‘,co[‘book‘][‘isbn10‘]
        print ‘author:‘,co[‘book‘][‘author‘][0]
        print ‘bookid:‘,co[‘book_id‘]
        print ‘id:‘,co[‘id‘]
        print ‘---------------------------‘

book_html = r‘https://api.douban.com/v2/book/user/AweSomeEthan/collections?status=read&start={pg}‘
try:
    book_json = json.loads(urllib2.urlopen(book_html.format(pg=0)).read())
    time.sleep(6)
except Exception as e1:
    print e1
for key in book_json:
    print key
book_p(book_json)
p = math.ceil(book_json[‘total‘]/float(book_json[‘count‘]))
t = 2
while t<=p:
    try:
        b_json = json.loads(urllib2.urlopen(book_html.format(pg=(t-1)*20)).read())
        time.sleep(6)
    except Exception as e:
        print e
    book_p(b_json)
    t = t+1

看结果:>>>
count
start
total
collections
count 20
start: 0
total 79
---------------------------
title: 月亮与六便士
isbn10: 7561383258
author: 【英】威廉· 萨摩赛特 毛姆
bookid: 26707463
id: 1041173580
---------------------------
title: 英雄志(全三册)
isbn10: 7806007679
author: 孙晓
bookid: 1109968
id: 976611344
---------------------------
title: 陈寅恪的最后20年
isbn10: 7108008041
author: 陆键东
bookid: 1002690
id: 740516509
---------------------------
title: 卖桔者言
isbn10: 7220003471
author: 张五常
bookid: 1438948
id: 805500875
---------------------------
title: 知识笔记
isbn10: 7508629558
author: 朱锡庆
bookid: 6798703
id: 805500485
---------------------------
title: 纸牌屋
isbn10: 7550008493
author: 迈克尔·道布斯
bookid: 25808056
id: 790958138
---------------------------
title: 梁启超谈儒学
isbn10: 7562245398
author: 伍渔
bookid: 5348969
id: 790956604
---------------------------
title: 策略
isbn10: 7543217481
author: Joel Watson[乔尔 沃森]
bookid: 5405599
id: 484533199
---------------------------
title: 一生伏首拜阳明
isbn10: 7547701574
author: 鹤阑珊
bookid: 6751277
id: 444315509
---------------------------
title: 漫步华尔街
isbn10: 7111235665
author: 麦基尔
bookid: 3043970
id: 501683900
---------------------------
title: 自控力
isbn10: 7514205039
author: [美]  凯利·麦格尼格尔
bookid: 10786473
id: 602679619
---------------------------
title: 历史深处的忧虑
isbn10: 7108010186
author: 林达
bookid: 1027191
id: 437091725
---------------------------
title: 民主的细节
isbn10: 7542629581
author: 刘瑜
bookid: 3813669
id: 477339868
---------------------------
title: 乡土中国
isbn10: 7208062153
author: 费孝通
bookid: 1795079
id: 740516632
---------------------------
title: 旧制度与大革命
isbn10: 7100022045
author: [法] 托克维尔
bookid: 1032063
id: 740517644
---------------------------
title: 小逻辑
isbn10: 7100005582
author: (德)黑格尔
bookid: 1034259
id: 502290875
---------------------------
title: 沉思录
isbn10: 7108028697
author: [古罗马] 马可·奥勒留·安东尼
bookid: 2375835
id: 740517124
---------------------------
title: 中国近代史
isbn10: 7532526615
author: 蒋廷黻
bookid: 1060025
id: 740516878
---------------------------
title: 中国建筑史
isbn10: 7530626051
author: 梁思成
bookid: 1009270
id: 740516740
---------------------------
title: 苏东坡传
isbn10: 7530630024
author: 林语堂
bookid: 1000614
id: 740516436
---------------------------
count 20
start: 20
total 79
---------------------------
title: 庄子
isbn10: 7805984735
author: 王岩峻
bookid: 1287864
id: 740516329
---------------------------
title: 精神分析引论
isbn10: 7100003156
author: [奥]西格蒙德·弗洛伊德
bookid: 1038292
id: 740516312
---------------------------
title: 彼得·林奇的成功投资
isbn10: 7111200500
author: [美] 彼得·林奇
bookid: 1958714
id: 740516279
---------------------------
title: 三国志(全五册)
isbn10: 7101003079
author: [晋] 陈寿 撰
bookid: 1014763
id: 740516232
---------------------------
title: 论美国的民主
isbn10: 7100011108
author: (法)托克维尔
bookid: 1041385
id: 740516178
---------------------------
title: 瓦尔登湖
isbn10: 7532739570
author: [美] 梭罗
bookid: 1865089
id: 740516001
---------------------------
title: 全球通史
isbn10: 7805150907
author: [美] L. S. 斯塔夫里阿诺斯
bookid: 1025643
id: 740515937
---------------------------
title: 国富论
isbn10: 7508036085
author: [英] 亚当·斯密
bookid: 1261560
id: 740515874
---------------------------
title: 社会契约论
isbn10: 7100037255
author: 卢梭
bookid: 1277981
id: 740515790
---------------------------
title: 诗经
isbn10: 720006503X
author: 孔丘 编订
bookid: 1883245
id: 740515717
---------------------------
title: 道德经
isbn10: 7224034819
author: 老子
bookid: 1059258
id: 740515668
---------------------------
title: 朝花夕拾
isbn10: SH10019-1985
author: 鲁迅
bookid: 1449352
id: 740515579
---------------------------
title: 理想国
isbn10: 7100017564
author: [古希腊] 柏拉图
bookid: 1004821
id: 740515511
---------------------------
title: 古文观止
isbn10: 7101000290
author: 吴楚材
bookid: 1014561
id: 740515413
---------------------------
title: 查拉图斯特拉如是说
isbn10: 754072501X
author: (德)尼采著
bookid: 1012032
id: 740515149
---------------------------
title: 人性的弱点全集
isbn10: 7800875830
author: [美] 戴尔·卡耐基
bookid: 1056295
id: 740515098
---------------------------
title: 我与地坛
isbn10: 7531324369
author: 史铁生
bookid: 1209899
id: 740515026
---------------------------
title: 中国近代史
isbn10: 7532543242
author: 蒋廷黻
bookid: 1823751
id: 740513540
---------------------------
title: 中国历代政治得失
isbn10: 7108015285
author: 钱穆
bookid: 1003479
id: 451858109
---------------------------
title: 万历十五年
isbn10: 7101052037
author: [美] 黄仁宇
bookid: 1858410
id: 740509383
---------------------------
count 20
start: 40
total 79
---------------------------
title: 非理性繁荣(第二版)
isbn10: 7300087698
author: 罗伯特·希勒
bookid: 3000660
id: 738114092
---------------------------
title: 先生
isbn10: 7508634438
author: 《先生》编写组
bookid: 11527058
id: 605674100
---------------------------
title: 中国近代史
isbn10: 7543067412
author: 蒋廷黻
bookid: 10575976
id: 605010508
---------------------------
title: 亨利五世
isbn10: 7100003660
author: 支荩忠 注释
bookid: 1011742
id: 602999689
---------------------------
title: 亨利四世
isbn10: SH10019-2533
author: 莎士比亚
bookid: 3278941
id: 602999457
---------------------------
title: 麦琪的礼物
isbn10: 7806812296
author: [美] 欧·亨利
bookid: 1051193
id: 602999402
---------------------------
title: 理查二世
isbn10: 7222058907
author: 威廉·莎士比亚
bookid: 4062866
id: 602999234
---------------------------
title: 麦克白
isbn10: 7507820254
author: [英] 莎士比亚
bookid: 1001065
id: 602999173
---------------------------
title: 仲夏夜之梦
isbn10: 7507820297
author: [英] 莎士比亚
bookid: 1012117
id: 602999145
---------------------------
title: 威尼斯商人
isbn10: 756001240X
author: (英)莎士比亚
bookid: 1034106
id: 602999127
---------------------------
title: 哈姆莱特
isbn10: 7020035949
author: [英] 莎士比亚
bookid: 1006197
id: 602999075
---------------------------
title: 了不起的盖茨比
isbn10: 7020046088
author: 菲茨杰拉德
bookid: 1008988
id: 602999055
---------------------------
title: 不能承受的生命之轻
isbn10: 7532731073
author: [捷克] 米兰·昆德拉
bookid: 1017143
id: 602999010
---------------------------
title: 第二次呼吸
isbn10: 7020093701
author: (法)博尔戈
bookid: 11528358
id: 591929930
---------------------------
title: 明史讲义
isbn10: 710105031X
author: 孟森
bookid: 1825043
id: 577769193
---------------------------
title: 国史新论
isbn10: 7108022400
author: 钱穆
bookid: 1277267
id: 577769072
---------------------------
title: 全球通史(第7版 上册)
isbn10: 7301074239
author: [美] 斯塔夫里阿诺斯
bookid: 1225977
id: 568401731
---------------------------
title: 中国大历史
isbn10: 7108010364
author: 黄仁宇
bookid: 1015699
id: 568401672
---------------------------
title: 晚清七十年 (全五冊)
isbn10: 9573235102
author: 唐德剛
bookid: 1424420
id: 568401586
---------------------------
title: 枪炮、病菌与钢铁
isbn10: 7532739236
author: [美] 贾雷德·戴蒙德
bookid: 1813841
id: 568401540
---------------------------
count 20
start: 60
total 79
---------------------------
title: 國史大綱(上下)
isbn10: 7100017661
author: 錢穆
bookid: 1046492
id: 535084482
---------------------------
title: 月亮和六便士
isbn10: 7532739546
author: [英] 毛姆
bookid: 1858513
id: 568399631
---------------------------
title: 一九八四·动物农场
isbn10: 7532729354
author: [英] 乔治·奥威尔
bookid: 1082387
id: 504456240
---------------------------
title: 追风筝的人
isbn10: 7208061645
author: [美] 卡勒德·胡赛尼
bookid: 1770782
id: 504456188
---------------------------
title: 货币战争
isbn10: 7508608682
author: 宋鸿兵 编著
bookid: 2081876
id: 504454628
---------------------------
title: 伟大的博弈
isbn10: 7508601637
author: [美] 约翰·S. 戈登
bookid: 1220853
id: 484700163
---------------------------
title: 青春
isbn10: 7543877457
author: 韩寒
bookid: 6902639
id: 477338486
---------------------------
title: 明朝那些事儿(壹)
isbn10: 7505722468
author: 当年明月
bookid: 1873231
id: 451858242
---------------------------
title: 呼啸山庄
isbn10: 7020027601
author: 艾米莉·勃朗特
bookid: 1119522
id: 403760696
---------------------------
title: 围城
isbn10: 7020024750
author: 钱锺书
bookid: 1008145
id: 403760628
---------------------------
title: 平凡的世界(全三部)
isbn10: 702004929X
author: 路遥
bookid: 1200840
id: 403760537
---------------------------
title: 达·芬奇密码
isbn10: 7208050031
author: [美] 丹·布朗
bookid: 1040771
id: 403760351
---------------------------
title: 肖申克的救赎
isbn10: 7020054986
author: [美] 斯蒂芬·金
bookid: 1829226
id: 403760300
---------------------------
title: 万历十五年
isbn10: 710800982X
author: [美] 黄仁宇
bookid: 1041482
id: 403760082
---------------------------
title: 明朝那些事儿(1-9)
isbn10: 7801656083
author: 当年明月
bookid: 3674537
id: 403759962
---------------------------
title: 明朝那些事儿(陆)
isbn10: 780165501X
author: 当年明月
bookid: 3274113
id: 403759923
---------------------------
title: 狂人日记
isbn10: 7807240644
author: 鲁迅
bookid: 1398395
id: 403759751
---------------------------
title: 呐喊
isbn10: SH10019-1979
author: 鲁迅
bookid: 1449351
id: 403759686
---------------------------
title: 傲慢与偏见
isbn10: 7532719111
author: [英] 奥斯丁
bookid: 1401841
id: 403759637
---------------------------
都列出来显得有点繁琐,哈哈。然后我们打开https://book.douban.com/people/AweSomeEthan/collect对照,完全吻合有木有,看来这个接口可以获取用户读过的书(用户是我从爬的用户里面随便拿的一个,绝对没有侵犯隐私的意思,所以如果主人公看到了还请见谅)。既然可以,我们优化一下代码:

#---coding:utf-8---

import urllib2
import json
import string
import time
import math

def book_print(book_json):
    ‘print book_info‘
    print ‘count‘,book_json[‘count‘]
    print ‘start:‘,book_json[‘start‘]
    print ‘total‘,book_json[‘total‘]
    print ‘---------------------------‘
    for co in book_json[‘collections‘]:
        print ‘title:‘,co[‘book‘][‘title‘]
        print ‘isbn10:‘,co[‘book‘][‘isbn10‘]
        #print ‘isbn13:‘,co[‘book‘][‘isbn13‘]
        print ‘author:‘,co[‘book‘][‘author‘][0]
        print ‘bookid:‘,co[‘book_id‘]
        print ‘id:‘,co[‘id‘]
        print ‘---------------------------‘
def get_bookpage(uid):
    ‘获取参加活动用户读过的书的总数‘
    book_html = r‘https://api.douban.com/v2/book/user/‘+uid+‘/collections?status=read&start={pg}‘
    try:
        book_json = json.loads(urllib2.urlopen(book_html.format(pg=0)).read())
        waittime()
    except Exception as ex:
        print ex
    p = math.ceil(book_json[‘total‘]/float(book_json[‘count‘]))
    get_book(uid,p)

def get_book(uid,page):
    ‘获取用户读过的书‘
    book_html = r‘https://api.douban.com/v2/book/user/‘+uid+‘/collections?status=read&start={pg}‘
    t = 0
    print ‘用户读过的书:‘
    while t<page:
        try:
            book_json = json.loads(urllib2.urlopen(book_html.format(pg=t*20)).read())
            waittime()
        except Exception as e:
            print e
        book_print(book_json)
        t = t+1
def waittime():
    ‘等待函数‘
    time.sleep(6)

def main():
    print ‘begin==========================‘

    get_bookpage(AweSomeEthan)

    print ‘==========================OVER!‘

if __name__ == ‘__main__‘:
    main()

等待函数是为了防止IP被封(因为网站对访问次数有限制,过于频繁IP会被拉黑),没有key每分钟限制10次,所以等6秒。uid(接口里参数是:name)可以是id,也可以是uid,可以提供API获取用户,当然方式有很多种(比如通过一个活动)。
那么我们不用API可不可以,直接从https://book.douban.com/people/AweSomeEthan/collect网页抓,下篇我们用beautifulsoup试一下。(©seven_clear)

时间: 2024-08-01 22:42:00

Python获取豆瓣用户读过的书的相关文章

Python 获取Facebook用户的Friends的爱好

Code: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-11 @author: guaguastd @name: friends_like_query.py ''' # impot login from login import facebook_login # import helper from helper import pp # access to facebook facebook_api = face

Python 获取Twitter用户与Friends和Followers的关系(eg, 交集,差集)

CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-30 @author: guaguastd @name: friends_followers_analyzer.py ''' if __name__ == '__main__': # import json #import json # import search from search import search_for_tweet # import ge

Python 获取Facebook用户与其有共同爱好的Friends

CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-13 @author: guaguastd @name: common_friends_finding.py ''' # impot login from login import facebook_login # import helper #from helper import pp # import PrettyTable from prettytab

Python 获取Twitter用户的所有的friends和followers

CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-29 @author: guaguastd @name: friends_followers_fetch.py ''' if __name__ == '__main__': # import json import json # import search from search import search_for_tweet # import get_fr

Python 获取Facebook用户的Friends的爱好中的Top10

CODE; #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: friends_popular_likes.py ''' # impot login from login import facebook_login # import helper from helper import pp # calculating the most popular likes

Python 获取Facebook用户Friends的爱好类别中的Top10

CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: friends_popular_category.py ''' # impot login from login import facebook_login # import helper #from helper import pp # calculating the most popular ca

python获取豆瓣电影TOP250的所有电影的相关信息

参考文档:https://weread.qq.com/web/reader/37132a705e2b2f37196c138k98f3284021498f137082c2e 说明:我才接触网络爬虫,在看<python网络爬虫入门到实践>一书时,作者写了个实例获取豆瓣电影TOP250的所有电影的电影名称,我在此基础上进行了更进一步的改进,获取了所有的相关信息,并用表格将这些信息保存下来. 相关知识: 网络爬虫分为三个步骤: 第一步:伪装为浏览器访问:第二步:解析网页代码:第三步:存储数据. (1)

[Python]豆瓣用户读书短评下载工具

简介 朋友问我能不能做一个下载他在豆瓣读书上的短评的工具,于是就做了这个“豆瓣用户读书短评下载工具”. GitHub链接:https://github.com/xiaff/dbc-downloader. 这个小工具使用Python3.4编写,其工作流程为: 用户输入其豆瓣ID: 抓取用户评论列表网页 对网页进行解析: 存储评论相关信息: 将Markdown格式文件转换为Html. 用到的库主要有: urllib.request BeautifulSoup4 markdown 抓取网页 所需要抓取

[Python]从豆瓣电影批量获取看过这部电影的用户列表

前言 由于之后要做一个实验,需要用到大量豆瓣用户的电影数据,因此想到了从豆瓣电影的“看过这部电影 的豆瓣成员”页面上来获取较为活跃的豆瓣电影用户. 链接分析 这是看过"模仿游戏"的豆瓣成员的网页链接:http://movie.douban.com/subject/10463953/collections. 一页上显示了20名看过这部电影的豆瓣用户.当点击下一页时,当前连接变为:http://movie.douban.com/subject/10463953/collections?st