zoukankan      html  css  js  c++  java
  • python爬取豆瓣电影信息数据

     题外话+

    大家好啊,最近自己在做一个属于自己的博客网站(准备辞职回家养老了,明年再战)在家里 琐事也很多, 加上自己 一回到家就懒了(主要是家里冷啊! 广东十几度,老家几度,躲在被窝瑟瑟发抖,)

    由于在建博客,也会遇到一些问题,我目前的博客发展就是 在创建博客的过程中, 把遇到的问题及解决方法  给说明出来,python 呢, 我也几周没玩了,,估计又忘记了(哎)好烦

    我看了一下, 博客最新文章是讲python 的 爬去图片的 

    我今天说说爬去豆瓣数据写入(我这里写入 txt)

    爬取豆瓣电影首页数据
    需要注意反扒问题,需要在header中加入referer

    import requests
    import json
    import re
    需要使用这三个库文件

    这里需要注意一个地方 (url 地址改变了 我这里使用浏览器切换为手机版,)
    找到数据

    
    
     这里有三个数据 ,对应一下




    大家发现规律了没???


    就是这样的 ,然后我们查看源代码找一下结构

    接下来把他们的请求连接拿下来

    Request URL:
    https://m.douban.com/rexxar/api/v2/subject_collection/movie_free_stream/items?os=ios&callback=jsonp2&start=0
    
    
    Request URL:
    
    
    https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items?os=ios&callback=jsonp1&start=0&count=8&loc_id=108288&_=1545982393545
    
    
    Request URL:
    
    
    https://m.douban.com/rexxar/api/v2/subject_collection/movie_latest/items?os=ios&callback=jsonp3&start=0&count=8&loc_id=108288&_=1545982393547
    这里的url 和我们id 一样啊???

    不管了 来个 正则
    reg = r'<section id="(.*?)">'
     
    index_Url = "https://m.douban.com/movie/"
        type = requests.get(index_Url).text
        # print(type) #获取整个html页面
        reg = r'<section id="(.*?)">'
        section = re.findall(reg, type, re.S | re.M)
        print(section)
        
    
    

    报错   我们说了 豆瓣反扒

     headers = {
            'Referer': 'https://m.douban.com/movie/',
            'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
        }
        params = {
            'os': 'ios',
            'start': '0',
            'count': '8',
            'loc_id': '108288'
        }

    再来看一下

     把这些参数都加上

    再试试
    index_Url = "https://m.douban.com/movie/"
        headers = {
            'Referer': 'https://m.douban.com/movie/',
            'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
        }
        params = {
            'os': 'ios',
            'start': '0',
            'count': '8',
            'loc_id': '108288'
        }
        type = requests.get(index_Url).text
        # print(type) #获取整个html页面
        reg = r'<section id="(.*?)">'
        section = re.findall(reg, type, re.S | re.M)
        print(section)
        
    结果 
    ['movie_showing', 'movie_free_stream', 'movie_latest']

    再来
    print("正在爬取" + section[0] + "的电影,请稍候...")
        data_url = "https://m.douban.com/rexxar/api/v2/subject_collection/" + section[0] + "/items?"
        list_data = requests.get(data_url, headers=headers, params=params).json()
        # print(list_data) #得出数据

    你这里可以弄个for 循环 循环 三次 调用 然后就可以 得到三类数据
    正在爬取movie_showing的电影,请稍候...
    {'count': 8, 'subject_collection': {'subject_count': 0, 'name': '影院热映', 'short_name': '', 'collect_count': 0, 'url': 'https://m.douban.com/app_topic/movie_showing', 'description': '', 'type_icon_bg_text': '', 'uri': 'douban://douban.com/subject_collection/movie_showing', 'cover_url': '', 'updated_at': None, 'icon_fg_image': '', 'type_text': '', 'show_header_mask': False, 'mini_program_name': '', 'id': 'movie_showing', 'medium_name': '', 'sharing_url': 'https://www.douban.com/doubanapp/dispatch?uri=/subject_collection/movie_showing/', 'mini_program_page': '', 'display': {'layout': 'grid'}, 'background_color_scheme': {'is_dark': True, 'primary_color_light': '6f6672', 'secondary_color': 'f8f4f9', 'primary_color_dark': '4a444c'}}, 'subject_collection_items': [{'original_price': None, 'rating': {'count': 107906, 'max': 10, 'value': 8.8}, 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2542867516.jpg', 'width': 2200, 'shape': 'rectangle', 'height': 3123}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 美国 / 动作 科幻 动画 / 鲍勃·佩尔西凯蒂 彼得·拉姆齐 罗德尼·罗斯曼 / 沙梅克·摩尔 杰克·约翰逊', 'id': '26374197', 'title': '蜘蛛侠:平行宇宙', 'comments': [{'comment': '谁能想到索尼居然在2018年的最后一个月用一部超级英雄动画挑战了迪士尼在动画奖项上的霸权?
    虽然主题上还是老调重弹,但动画在内容和形式上的选...-- 我是所有人的BFF', 'rating': {'count': 1, 'max': 5, 'star_count': 4, 'value': 4}, 'sharing_url': 'https://www.douban.com/doubanapp/dispatch?uri=/movie/26374197/interest/1530763193', 'is_voted': False, 'uri': 'douban://douban.com/movie/26374197/interest/1530763193', 'platforms': [], 'vote_count': 2795, 'create_time': '2018-12-02 17:20:57', 'status': 'done', 'user': {'loc': {'id': '128097', 'name': 'Melbourne', 'uid': 'melbourne'}, 'kind': 'user', 'followed': False, 'name': '我是所有人的BFF', 'in_blacklist': False, 'url': 'https://www.douban.com/people/138907653/', 'remark': '', 'uri': 'douban://douban.com/user/138907653', 'avatar': 'https://img3.doubanio.com/icon/up138907653-3.jpg', 'type': 'user', 'id': '138907653', 'uid': '138907653'}, 'done_index': 531, 'id': '1530763193'}], 'label': None, 'actors': ['沙梅克·摩尔', '杰克·约翰逊', '海莉·斯坦菲尔德'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '美国 / 动作 科幻 动画 / 鲍勃·佩尔西凯蒂 / 沙梅克·摩尔 杰克·约翰逊', 'rating_data': {'stats': [0.0019222297865524008, 0.007728965600096112, 0.0968723719514637, 0.38060149773737534, 0.5128749349245124], 'type_ranks': [{'type': '动画片', 'rank': 0.94}, {'type': '科幻片', 'rank': 0.98}]}, 'url': 'https://www.douban.com/doubanapp/dispatch/movie/26374197', 'release_date': '12.21', 'original_title': 'Spider-Man: Into the Spider-Verse', 'uri': 'douban://douban.com/movie/26374197', 'subtype': '', 'directors': ['鲍勃·佩尔西凯蒂'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': None, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 中国大陆 / 剧情 喜剧 / 于淼 / 佟大为 马丽', 'id': '30377703', 'title': '来电狂响', 'label': None, 'actors': ['佟大为', '马丽', '霍思燕'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '中国大陆 / 剧情 喜剧 / 于淼 / 佟大为 马丽', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/30377703', 'release_date': '12.28', 'cover': {'url': 'https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2542268337.jpg', 'width': 800, 'shape': 'rectangle', 'height': 1120}, 'uri': 'douban://douban.com/movie/30377703', 'subtype': '', 'directors': ['于淼'], 'reviewer_name': '', 'null_rating_reason': '暂无评分'}, {'original_price': None, 'rating': {'count': 339248, 'max': 10, 'value': 7.9}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 美国 澳大利亚 / 动作 奇幻 冒险 / 温子仁 / 杰森·莫玛 艾梅柏·希尔德', 'id': '3878007', 'title': '海王', 'label': None, 'actors': ['杰森·莫玛', '艾梅柏·希尔德', '威廉·达福'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '美国 澳大利亚 / 动作 奇幻 冒险 / 温子仁 / 杰森·莫玛 艾梅柏·希尔德', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/3878007', 'release_date': '12.07', 'cover': {'url': 'https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2541280047.jpg', 'width': 6750, 'shape': 'rectangle', 'height': 10000}, 'uri': 'douban://douban.com/movie/3878007', 'subtype': '', 'directors': ['温子仁'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 483, 'max': 10, 'value': 6.7}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 印度 / 喜剧 动作 冒险 / 维贾伊·克利须那·阿查里雅 / 阿米尔·汗 卡特莉娜·卡芙', 'id': '27019982', 'title': '印度暴徒', 'label': None, 'actors': ['阿米尔·汗', '卡特莉娜·卡芙', '阿米达普·巴强'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '印度 / 喜剧 动作 冒险 / 维贾伊·克利须那·阿查里雅 / 阿米尔·汗 卡特莉娜·卡芙', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/27019982', 'release_date': '12.28', 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2541754700.jpg', 'width': 5906, 'shape': 'rectangle', 'height': 8268}, 'uri': 'douban://douban.com/movie/27019982', 'subtype': '', 'directors': ['维贾伊·克利须那·阿查里雅'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 1403, 'max': 10, 'value': 7.0}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 美国 / 剧情 / 布兰登·坎普 / 加布里埃尔·贝特曼 达比·坎普', 'id': '26895438', 'title': '神探狗笨吉', 'label': None, 'actors': ['加布里埃尔·贝特曼', '达比·坎普', '基拉·桑切斯'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '美国 / 剧情 / 布兰登·坎普 / 加布里埃尔·贝特曼 达比·坎普', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/26895438', 'release_date': '12.28', 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2541725145.jpg', 'width': 714, 'shape': 'rectangle', 'height': 1000}, 'uri': 'douban://douban.com/movie/26895438', 'subtype': '', 'directors': ['布兰登·坎普'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 10150, 'max': 10, 'value': 4.0}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 中国大陆 / 喜剧 奇幻 / 肖央 / 肖央 杜鹃', 'id': '26994789', 'title': '天气预爆', 'label': None, 'actors': ['肖央', '杜鹃', '常远'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '中国大陆 / 喜剧 奇幻 / 肖央 / 肖央 杜鹃', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/26994789', 'release_date': '12.21', 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2543353290.jpg', 'width': 2142, 'shape': 'rectangle', 'height': 3000}, 'uri': 'douban://douban.com/movie/26994789', 'subtype': '', 'directors': ['肖央'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 5088, 'max': 10, 'value': 5.8}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 中国大陆 香港 / 动作 / 袁和平 / 张晋 戴夫·巴蒂斯塔', 'id': '26796664', 'title': '叶问外传:张天志', 'label': None, 'actors': ['张晋', '戴夫·巴蒂斯塔', '柳岩'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '中国大陆 香港 / 动作 / 袁和平 / 张晋 戴夫·巴蒂斯塔', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/26796664', 'release_date': '12.21', 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2542380253.jpg', 'width': 1080, 'shape': 'rectangle', 'height': 1512}, 'uri': 'douban://douban.com/movie/26796664', 'subtype': '', 'directors': ['袁和平'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 594320, 'max': 10, 'value': 9.1}, 'actions': [], 'year': '1988', 'card_subtitle': '1988 / 日本 / 动画 奇幻 冒险 / 宫崎骏 / 日高法子 坂本千夏', 'id': '1291560', 'title': '龙猫', 'label': None, 'actors': ['日高法子', '坂本千夏', '糸井重里'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '日本 / 动画 奇幻 冒险 / 宫崎骏 / 日高法子 坂本千夏', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/1291560', 'release_date': '12.14', 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2540924496.jpg', 'width': 1600, 'shape': 'rectangle', 'height': 2358}, 'uri': 'douban://douban.com/movie/1291560', 'subtype': '', 'directors': ['宫崎骏'], 'reviewer_name': '', 'null_rating_reason': ''}], 'total': 41, 'start': 0}
    爬取完毕
    ['movie_showing', 'movie_free_stream', 'movie_latest']
    正在爬取movie_free_stream的电影,请稍候...
    {'count': 8, 'subject_collection': {'subject_count': 0, 'name': '免费在线观看的新片', 'short_name': '', 'collect_count': 0, 'url': 'https://m.douban.com/app_topic/movie_free_stream', 'description': '', 'type_icon_bg_text': '', 'uri': 'douban://douban.com/subject_collection/movie_free_stream', 'cover_url': '', 'updated_at': None, 'icon_fg_image': '', 'type_text': '', 'show_header_mask': False, 'mini_program_name': '', 'id': 'movie_free_stream', 'medium_name': '', 'sharing_url': 'https://www.douban.com/doubanapp/dispatch?uri=/subject_collection/movie_free_stream/', 'mini_program_page': '', 'display': {'layout': 'grid'}, 'background_color_scheme': {'is_dark': True, 'primary_color_light': '410b72', 'secondary_color': 'f7f4f9', 'primary_color_dark': '2b074c'}}, 'subject_collection_items': [{'original_price': None, 'rating': {'count': 200, 'max': 10, 'value': 6.0}, 'cover': {'url': 'https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2539179937.jpg', 'width': 450, 'shape': 'rectangle', 'height': 800}, 'actions': ['可播放'], 'year': '2018', 'card_subtitle': '2018 / 中国大陆 / 歌舞 真人秀 / 陈赫 黄渤', 'id': '30371772', 'title': '2018天猫双十一狂欢夜', 'comments': [{'comment': '???-- 谢谢你们的鱼', 'rating': {'count': 1, 'max': 5, 'star_count': 1, 'value': 1}, 'sharing_url': 'https://www.douban.com/doubanapp/dispatch?uri=/movie/30371772/interest/1520203091', 'is_voted': False, 'uri': 'douban://douban.com/movie/30371772/interest/1520203091', 'platforms': [], 'vote_count': 4, 'create_time': '2018-11-14 15:15:19', 'status': 'done', 'user': {'loc': {'id': '108288', 'name': '北京', 'uid': 'beijing'}, 'kind': 'user', 'followed': False, 'name': '谢谢你们的鱼', 'in_blacklist': False, 'url': 'https://www.douban.com/people/1752908/', 'remark': '', 'uri': 'douban://douban.com/user/1752908', 'avatar': 'https://img1.doubanio.com/icon/up1752908-27.jpg', 'type': 'user', 'id': '1752908', 'uid': 'reave'}, 'done_index': 6934, 'id': '1520203091'}], 'label': None, 'actors': ['陈赫', '黄渤', '吴谨言'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': True, 'price': None, 'date': None, 'info': '中国大陆 / 歌舞 真人秀 / 陈赫 黄渤', 'rating_data': {'stats': [0.125, 0.16666666666666666, 0.4583333333333333, 0.08333333333333333, 0.16666666666666666], 'type_ranks': []}, 'url': 'https://www.douban.com/doubanapp/dispatch/movie/30371772', 'release_date': '11.10', 'original_title': '', 'uri': 'douban://douban.com/movie/30371772', 'subtype': '', 'directors': [], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 1523, 'max': 10, 'value': 8.7}, 'actions': ['可播放'], 'year': '2018', 'card_subtitle': '2018 / 日本 / 科幻 动画 / 安彦良和 / 池田秀一 潘惠美', 'id': '26920182', 'title': '机动战士高达THE ORIGIN VI 赤色彗星诞生', 'label': None, 'actors': ['池田秀一', '潘惠美', '银河万丈'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': True, 'price': None, 'date': None, 'info': '日本 / 科幻 动画 / 安彦良和 / 池田秀一 潘惠美', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/26920182', 'release_date': '05.05', 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2518242401.jpg', 'width': 654, 'shape': 'rectangle', 'height': 918}, 'uri': 'douban://douban.com/movie/26920182', 'subtype': '', 'directors': ['安彦良和'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 545, 'max': 10, 'value': 4.6}, 'actions': ['可播放'], 'year': '2018', 'card_subtitle': '2018 / 中国大陆 / 剧情 爱情 悬疑 / 杨俊汉 / 宋宁峰 吕星辰', 'id': '30353414', 'title': '凌晨四点的重庆', 'label': None, 'actors': ['宋宁峰', '吕星辰', '何泓姗'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': True, 'price': None, 'date': None, 'info': '中国大陆 / 剧情 爱情 悬疑 / 杨俊汉 / 宋宁峰 吕星辰', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/30353414', 'release_date': '10.16', 'cover': {'url': 'https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2537701268.jpg', 'width': 1969, 'shape': 'rectangle', 'height': 3500}, 'uri': 'douban://douban.com/movie/30353414', 'subtype': '', 'directors': ['杨俊汉'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 527, 'max': 10, 'value': 7.8}, 'actions': ['可播放'], 'year': '2018', 'card_subtitle': '2018 / 中国大陆 / 爱情 动画 / 金承仁 / 陈张太康 佟心竹', 'id': '30156266', 'title': '我是江小白 剧场版', 'label': None, 'actors': ['陈张太康', '佟心竹', '李兰陵'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': True, 'price': None, 'date': None, 'info': '中国大陆 / 爱情 动画 / 金承仁 / 陈张太康 佟心竹', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/30156266', 'release_date': '05.25', 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2523138791.jpg', 'width': 650, 'shape': 'rectangle', 'height': 1048}, 'uri': 'douban://douban.com/movie/30156266', 'subtype': '', 'directors': ['金承仁'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 37981, 'max': 10, 'value': 2.0}, 'actions': ['可播放'], 'year': '2018', 'card_subtitle': '2018 / 中国大陆 / 真人秀 / 何炅 迪丽热巴', 'id': '30352998', 'title': '第12届中国金鹰电视艺术节颁奖典礼', 'label': None, 'actors': ['何炅', '迪丽热巴', '李易峰'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': True, 'price': None, 'date': None, 'info': '中国大陆 / 真人秀 / 何炅 迪丽热巴', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/30352998', 'release_date': '10.12', 'cover': {'url': 'https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2539033188.jpg', 'width': 1461, 'shape': 'rectangle', 'height': 2019}, 'uri': 'douban://douban.com/movie/30352998', 'subtype': '', 'directors': [], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 934, 'max': 10, 'value': 6.8}, 'actions': ['可播放'], 'year': '2018', 'card_subtitle': '2018 / 中国大陆 / 动画 短片 / Andrew Chesworth(安德鲁•查斯沃斯) Bobby Pontillas(巴比•庞迪亚斯)', 'id': '30217440', 'title': '冲破天际', 'label': None, 'actors': [], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': True, 'price': None, 'date': None, 'info': '中国大陆 / 动画 短片 / Andrew Chesworth(安德鲁•查斯沃斯) / ', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/30217440', 'release_date': '06.24', 'cover': {'url': 'https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2534695097.jpg', 'width': 960, 'shape': 'rectangle', 'height': 1440}, 'uri': 'douban://douban.com/movie/30217440', 'subtype': '', 'directors': ['Andrew Chesworth(安德鲁•查斯沃斯)'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 526, 'max': 10, 'value': 8.4}, 'actions': ['可播放'], 'year': '2018', 'card_subtitle': '2018 / 中国大陆 / 纪录片 / 陈鹤庭 / 穆飞 高鸣', 'id': '30290809', 'title': '独行', 'label': None, 'actors': ['穆飞', '高鸣', '李远扬'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': True, 'price': None, 'date': None, 'info': '中国大陆 / 纪录片 / 陈鹤庭 / 穆飞 高鸣', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/30290809', 'release_date': '08.18', 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2531116444.jpg', 'width': 1346, 'shape': 'rectangle', 'height': 1984}, 'uri': 'douban://douban.com/movie/30290809', 'subtype': '', 'directors': ['陈鹤庭'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 1126, 'max': 10, 'value': 3.2}, 'actions': ['可播放'], 'year': '2018', 'card_subtitle': '2018 / 中国大陆 / 动作 冒险 / 林珍钊 / 黄凯伦 郑跃', 'id': '30306436', 'title': '大蛇', 'label': None, 'actors': ['黄凯伦', '郑跃', '奚美丽'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': True, 'price': None, 'date': None, 'info': '中国大陆 / 动作 冒险 / 林珍钊 / 黄凯伦 郑跃', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/30306436', 'release_date': '09.04', 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2531890522.jpg', 'width': 435, 'shape': 'rectangle', 'height': 615}, 'uri': 'douban://douban.com/movie/30306436', 'subtype': '', 'directors': ['林珍钊'], 'reviewer_name': '', 'null_rating_reason': ''}], 'total': 16, 'start': 0}
    爬取完毕
    ['movie_showing', 'movie_free_stream', 'movie_latest']
    正在爬取movie_latest的电影,请稍候...
    {'count': 8, 'subject_collection': {'subject_count': 0, 'name': '近期热门电影', 'short_name': '', 'collect_count': 0, 'url': 'https://m.douban.com/app_topic/movie_latest', 'description': '', 'type_icon_bg_text': '', 'uri': 'douban://douban.com/subject_collection/movie_latest', 'cover_url': '', 'updated_at': None, 'icon_fg_image': '', 'type_text': '', 'show_header_mask': False, 'mini_program_name': '', 'id': 'movie_latest', 'medium_name': '', 'sharing_url': 'https://www.douban.com/doubanapp/dispatch?uri=/subject_collection/movie_latest/', 'mini_program_page': '', 'display': {'layout': 'grid'}, 'background_color_scheme': {'is_dark': True, 'primary_color_light': 'a58a6e', 'secondary_color': 'f9f7f4', 'primary_color_dark': '7f6a55'}}, 'subject_collection_items': [{'original_price': None, 'rating': {'count': 312, 'max': 10, 'value': 5.7}, 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2517978315.jpg', 'width': 848, 'shape': 'rectangle', 'height': 1200}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 日本 / 剧情 惊悚 / 绫部真弥 / 武田玲奈 上野优华', 'id': '27608249', 'title': '人狼游戏:地狱', 'comments': [{'comment': '敲黑板提醒:这次是TV版+电影版组合放送,TV版《人狼游戏:走失伊甸》有非常关键情节且与电影版《人狼游戏:地狱》无缝连接,为避免认知断层,一...-- 榜上有名猫', 'rating': {'count': 1, 'max': 5, 'star_count': 4, 'value': 4}, 'sharing_url': 'https://www.douban.com/doubanapp/dispatch?uri=/movie/27608249/interest/1317392030', 'is_voted': False, 'uri': 'douban://douban.com/movie/27608249/interest/1317392030', 'platforms': [], 'vote_count': 8, 'create_time': '2018-09-08 16:12:15', 'status': 'done', 'user': {'loc': {'id': '118281', 'name': '广州', 'uid': 'guangzhou'}, 'kind': 'user', 'followed': False, 'name': '榜上有名猫', 'in_blacklist': False, 'url': 'https://www.douban.com/people/77659294/', 'remark': '', 'uri': 'douban://douban.com/user/77659294', 'avatar': 'https://img3.doubanio.com/icon/up77659294-2.jpg', 'type': 'user', 'id': '77659294', 'uid': 'fang0079'}, 'done_index': 1712, 'id': '1317392030'}], 'label': None, 'actors': ['武田玲奈', '上野优华', '小仓优香'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '日本 / 剧情 惊悚 / 绫部真弥 / 武田玲奈 上野优华', 'rating_data': {'stats': [0.075, 0.2916666666666667, 0.4166666666666667, 0.14583333333333334, 0.07083333333333333], 'type_ranks': []}, 'url': 'https://www.douban.com/doubanapp/dispatch/movie/27608249', 'release_date': '04.07', 'original_title': '人狼ゲーム インフェルノ', 'uri': 'douban://douban.com/movie/27608249', 'subtype': '', 'directors': ['绫部真弥'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 335, 'max': 10, 'value': 5.8}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 美国 / 科幻 惊悚 / 塞巴斯蒂安·古提耶雷兹 / 阿比·丽 卡拉·古奇诺', 'id': '27073254', 'title': '收割伊丽莎白', 'label': None, 'actors': ['阿比·丽', '卡拉·古奇诺', '塞伦·希德'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '美国 / 科幻 惊悚 / 塞巴斯蒂安·古提耶雷兹 / 阿比·丽 卡拉·古奇诺', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/27073254', 'release_date': '03.10', 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2528233101.jpg', 'width': 1418, 'shape': 'rectangle', 'height': 2100}, 'uri': 'douban://douban.com/movie/27073254', 'subtype': '', 'directors': ['塞巴斯蒂安·古提耶雷兹'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 1389, 'max': 10, 'value': 6.6}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 韩国 / 悬疑 惊悚 / 李权 / 孔晓振 金叡园', 'id': '27617348', 'title': '门锁', 'label': None, 'actors': ['孔晓振', '金叡园', '金圣武'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '韩国 / 悬疑 惊悚 / 李权 / 孔晓振 金叡园', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/27617348', 'release_date': '12.05', 'cover': {'url': 'https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2537955628.jpg', 'width': 4234, 'shape': 'rectangle', 'height': 6066}, 'uri': 'douban://douban.com/movie/27617348', 'subtype': '', 'directors': ['李权'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 368, 'max': 10, 'value': 5.1}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 法国 比利时 / 动作 犯罪 惊悚 / 朱利安·勒克莱克 / 尚格·云顿  斯维娃·阿维蒂', 'id': '27190070', 'title': '保镖', 'label': None, 'actors': ['尚格·云顿', ' 斯维娃·阿维蒂', '塞米·鲍亚吉拉'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '法国 比利时 / 动作 犯罪 惊悚 / 朱利安·勒克莱克 / 尚格·云顿  斯维娃·阿维蒂', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/27190070', 'release_date': '08.22', 'cover': {'url': 'https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2527369948.jpg', 'width': 750, 'shape': 'rectangle', 'height': 1000}, 'uri': 'douban://douban.com/movie/27190070', 'subtype': '', 'directors': ['朱利安·勒克莱克'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 388, 'max': 10, 'value': 5.9}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 西班牙 法国 / 剧情 悬疑 / 阿斯哈·法哈蒂 / 哈维尔·巴登 佩内洛普·克鲁兹', 'id': '26601224', 'title': '人尽皆知', 'label': None, 'actors': ['哈维尔·巴登', '佩内洛普·克鲁兹', '里卡多·达林'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '西班牙 法国 / 剧情 悬疑 / 阿斯哈·法哈蒂 / 哈维尔·巴登 佩内洛普·克鲁兹', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/26601224', 'release_date': None, 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2529777486.jpg', 'width': 2100, 'shape': 'rectangle', 'height': 3000}, 'uri': 'douban://douban.com/movie/26601224', 'subtype': '', 'directors': ['阿斯哈·法哈蒂'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 1678, 'max': 10, 'value': 8.2}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 印度 / 喜剧 犯罪 悬疑 / 斯里兰姆·拉格万 / 阿尤斯曼·库拉纳 塔布', 'id': '30334073', 'title': '看不见的旋律', 'label': None, 'actors': ['阿尤斯曼·库拉纳', '塔布', '拉迪卡·艾普特'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '印度 / 喜剧 犯罪 悬疑 / 斯里兰姆·拉格万 / 阿尤斯曼·库拉纳 塔布', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/30334073', 'release_date': '10.05', 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2535089164.jpg', 'width': 692, 'shape': 'rectangle', 'height': 1000}, 'uri': 'douban://douban.com/movie/30334073', 'subtype': '', 'directors': ['斯里兰姆·拉格万'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 570, 'max': 10, 'value': 7.2}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 美国 / 剧情 喜剧 / 乔纳·希尔 / 桑尼·苏尔季克 凯瑟琳·沃特斯顿', 'id': '26762269', 'title': '90年代中期', 'label': None, 'actors': ['桑尼·苏尔季克', '凯瑟琳·沃特斯顿', '卢卡斯·赫奇斯'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '美国 / 剧情 喜剧 / 乔纳·希尔 / 桑尼·苏尔季克 凯瑟琳·沃特斯顿', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/26762269', 'release_date': '09.09', 'cover': {'url': 'https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2528895738.jpg', 'width': 962, 'shape': 'rectangle', 'height': 1500}, 'uri': 'douban://douban.com/movie/26762269', 'subtype': '', 'directors': ['乔纳·希尔'], 'reviewer_name': '', 'null_rating_reason': ''}, {'original_price': None, 'rating': {'count': 458, 'max': 10, 'value': 6.4}, 'actions': [], 'year': '2018', 'card_subtitle': '2018 / 日本 / 剧情 家庭 / 郑义信 / 真木阳子 井上真央', 'id': '27037052', 'title': '烧肉龙', 'label': None, 'actors': ['真木阳子', '井上真央', '樱庭奈奈美'], 'interest': None, 'type': 'movie', 'forum_info': None, 'description': '', 'has_linewatch': False, 'price': None, 'date': None, 'info': '日本 / 剧情 家庭 / 郑义信 / 真木阳子 井上真央', 'url': 'https://www.douban.com/doubanapp/dispatch/movie/27037052', 'release_date': '06.22', 'cover': {'url': 'https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2518053194.jpg', 'width': 566, 'shape': 'rectangle', 'height': 800}, 'uri': 'douban://douban.com/movie/27037052', 'subtype': '', 'directors': ['郑义信'], 'reviewer_name': '', 'null_rating_reason': ''}], 'total': 500, 'start': 0}
    data

    类似

    所属分类:影院热映
    电影名字:来电狂响
    主角佟大为
    电影出产处:中国大陆 / 剧情 喜剧 / 于淼 / 佟大为 马丽
    电影观看地址:https://www.douban.com/doubanapp/dispatch/movie/30377703

    得到数据 我们 就要 对比一下数据的特点 然后分析需要爬出哪些数据

    类似上面这样子

    我们格式化数据看看

    http://www.bejson.com/               推荐网站

    我们这个时候吧数据 复制过去 然后点击 格式化会报错的

     ;jsonp1({  });

    需要删除前面的 这个

    再点击格式化才ok

    j截取部分数据如下

    {
        "count": 8,
        "subject_collection": {   # 我们 发现数据 都在 他下面
            "subject_count": 0,
            "name": "影院热映",     # 获取 分类
            "short_name": "",
            "collect_count": 0,
            "url": "https://m.douban.com/app_topic/movie_showing",
            "description": "",
            "type_icon_bg_text": "",
            "uri": "douban://douban.com/subject_collection/movie_showing",
            "cover_url": "",
            "updated_at": null,
            "icon_fg_image": "",
            "type_text": "",
            "show_header_mask": false,
            "mini_program_name": "",
            "id": "movie_showing",
            "medium_name": "",
            "sharing_url": "https://www.douban.com/doubanapp/dispatch?uri=/subject_collection/movie_showing/",
            "mini_program_page": "",
            "display": {
                "layout": "grid"
            },
            "background_color_scheme": {
                "is_dark": true,
                "primary_color_light": "6f6672",
                "secondary_color": "f8f4f9",
                "primary_color_dark": "4a444c"
            }
        },
        "subject_collection_items": [{          # 然后 subhect_collection_item 下面的 subject_collection_items 
             "original_price": null,
            "rating": {
                "count": 107818,
                "max": 10,
                "value": 8.8
            },
            "cover": {
                "url": "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2542867516.jpg", #  图片路径
                "width": 2200,
                "shape": "rectangle",
                "height": 3123
            },
            "actions": [],
            "year": "2018",
            "card_subtitle": "2018 / 美国 / 动作 科幻 动画 / 鲍勃·佩尔西凯蒂 彼得·拉姆齐 罗德尼·罗斯曼 / 沙梅克·摩尔 杰克·约翰逊",  # 介绍
            "id": "26374197",
            "title": "蜘蛛侠:平行宇宙",
            "comments": [{
                "comment": "谁能想到索尼居然在2018年的最后一个月用一部超级英雄动画挑战了迪士尼在动画奖项上的霸权?
    虽然主题上还是老调重弹,但动画在内容和形式上的选...-- 我是所有人的BFF",   #介绍
                "rating": {
                    "count": 1,
                    "max": 5,
                    "star_count": 4,
                    "value": 4
                },
                "sharing_url": "https://www.douban.com/doubanapp/dispatch?uri=/movie/26374197/interest/1530763193",  # 播放地址
                "is_voted": false,
                "uri": "douban://douban.com/movie/26374197/interest/1530763193",
                "platforms": [],
                "vote_count": 2792,
                "create_time": "2018-12-02 17:20:57",
                "status": "done",
                "user": {
                    "loc": {
                        "id": "128097",
                        "name": "Melbourne",
                        "uid": "melbourne"
                    },
                    "kind": "user",
                    "followed": false,
                    "name": "我是所有人的BFF",

    #好了 就像获取这么 的  ,我已经 把  需要、主要的地方告诉你们了

    接下来 撸代码



        with  open("./豆瓣.txt", "a", encoding="utf-8") as  f:
            for i in range(0, int(len(list_data["subject_collection_items"]))):
                try:
                    data_write = "
    所属分类:" 
                                 + str(list_data["subject_collection"]["name"]) + 
                                 "
    电影名字:" + 
                                 str(list_data["subject_collection_items"][i]["title"]) + 
                                 "
    主角" + str(list_data["subject_collection_items"][i]["actors"][0]) + 
                                 "
    电影出产处:" + str(list_data["subject_collection_items"][i]["info"]) + 
                                 "
    电影观看地址:" + str(list_data["subject_collection_items"][i]["url"]) + 
                                 "
    ----------结束-------------"
                    '''  后面都差不多都是这种 格式
                    '''
                    f.write(
                        data_write
                    )
                except Exception as eee:
                    return eee
            # 这里会有一个错误  -->list index out of range
        # print(eee)
    我们在这里 加一个 print("数据爬去完毕") 提醒
    
    

    接下来 写个 方法 封装一下


    def Mian(iii):
      。。。


    调用

    前面我们不是 说了 嘛
    获取了三个id 需要 section[0]
    section[1]
    section[2]

    我们写个把for 循环写在外面

    for iii in range (0,3):
    Mian(iii)


    然后自己调用


    这篇文章 讲的 很快, 大多数 的确也很快, 假如 说了 还没说 正则
    上面的正则是 匹配 id 的 叫我 讲 我也讲不清楚

    我只能 把我 的思路告诉 你们 顺便把我的代码 发给你们参考一下(新手代码啊! 大佬 觉得有优化的 地方 直接撸 顺便把代码贴到这里啊!!!)
     
    import requests
    import json
    import re
    
    
    def Mian(iii):
        index_Url = "https://m.douban.com/movie/"
        headers = {
            'Referer': 'https://m.douban.com/movie/',
            'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
        }
        params = {
            'os': 'ios',
            'start': '0',
            'count': '8',
            'loc_id': '108288'
        }
        type = requests.get(index_Url).text
        # print(type) #获取整个html页面
        reg = r'<section id="(.*?)">'
        section = re.findall(reg, type, re.S | re.M)
        print(section)
        # ['movie_showing"', 'movie_free_stream"', 'movie_latest"']
        # 数据Url
        # https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items?os=ios&callback=jsonp1&start=0&count=8&loc_id=108288&_=1544928329165
        # https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items?os=ios&callback=jsonp1&start=0&count=8&loc_id=108288&_=1544928329165
        # https://m.douban.com/rexxar/api/v2/subject_collection/movie_latest/items?os=ios&callback=jsonp3&start=0&count=8&loc_id=108288&_=1544928329179
        print("正在爬取" + section[iii] + "的电影,请稍候...")
        data_url = "https://m.douban.com/rexxar/api/v2/subject_collection/" + section[iii] + "/items?"
        list_data = requests.get(data_url, headers=headers, params=params).json()
        # print(list_data) #得出数据
    
        with  open("./豆瓣.txt", "a", encoding="utf-8") as  f:
            for i in range(0, int(len(list_data["subject_collection_items"]))):
                try:
                    data_write = "
    所属分类:" 
                                 + str(list_data["subject_collection"]["name"]) + 
                                 "
    电影名字:" + 
                                 str(list_data["subject_collection_items"][i]["title"]) + 
                                 "
    主角" + str(list_data["subject_collection_items"][i]["actors"][0]) + 
                                 "
    电影出产处:" + str(list_data["subject_collection_items"][i]["info"]) + 
                                 "
    电影观看地址:" + str(list_data["subject_collection_items"][i]["url"]) + 
                                 "
    ----------结束-------------"
                    '''  后面都差不多是这样子  
                    '''
                    f.write(
                        data_write
                    )
                except Exception as eee:
                    return eee
            # 这里会有一个错误  -->list index out of range
        # print(eee)
    
        print("爬取完毕")
    
    
    for  iii  in   range (0,3):
        Mian(iii)
    View Code
    大家有什么不懂 也可以问我哈, 一起进步哟
    正在 做自己的博客

    加油




    拜拜我的少年时代!!!
  • 相关阅读:
    209. Minimum Size Subarray Sum
    208. Implement Trie (Prefix Tree)
    207. Course Schedule
    206. Reverse Linked List
    205. Isomorphic Strings
    204. Count Primes
    203. Remove Linked List Elements
    201. Bitwise AND of Numbers Range
    199. Binary Tree Right Side View
    ArcGIS API for JavaScript 4.2学习笔记[8] 2D与3D视图同步
  • 原文地址:https://www.cnblogs.com/whatarey/p/10191178.html
Copyright © 2011-2022 走看看