[Python]爬虫批量获取哔哩哔哩Coser的相册

最近老在弄硬件综合设计课程的实验,实现CPU,身心俱疲,所以上哔哩哔哩网站看了看发现个好东西…

哔哩哔哩网站顶部上有一个叫“画友”的功能,打开发现基本是一些 Coser 的照片,鉴于一篇篇的观赏过于麻烦,不如动手写个爬虫把妹子们放到磁盘里…

meizi

0x02 看看爬虫的效果:

执行过程

运行结果


0x03 直接放上源代码吧:

# -*- coding:utf-8 -*-
# @author: DYBOY
# @link: https://blog.dyboy.cn

import requests
import json
import os
import time


main_url = 'https://api.vc.bilibili.com/link_draw/v2/Photo/list?category=cos&type=hot&page_num='

req = requests.Session()

def get_html(page_num):
    header= {
        'Accept':'application/json, text/plain, */*',
        'Accept-Encoding':'gzip, deflate, br',
        'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8',
        'Connection':'keep-alive',
        'Cookie':' _dfcaptcha=2a3b6a18dc2f49833a6214509e784a6f; UM_distinctid=167c996fe832b-00aee39f185ce-3c604504-1fa400-167c996fe84325; CURRENT_QUALITY=16; fts=1545275440; bsource=seo_baidu'
        ,'Host':'api.vc.bilibili.com',
        'Origin':'https://h.bilibili.com',
        'Referer':'https://h.bilibili.com/eden/picture_area',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
        }

    html = req.get(main_url+str(page_num))
    html.encode = 'utf-8'
    return html.text

def create_dic(dic_name):
    curr_path = os.getcwd()
    temPath = curr_path+os.path.sep+dic_name
    if not os.path.exists(temPath):
        os.makedirs(temPath)
    else:
        print('文件夹已存在')
    return temPath


def save_pic(dic,imgsrc,name):
    img = requests.get(imgsrc)
    with open(dic+'/'+name,"wb") as f:
        f.write(img.content)
    return None

if __name__ == '__main__':
    print ("开始时间:"+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    for i in range(0,25):
        album_list = json.loads(get_html(i))
        print("第"+str(i)+"页")
        for album in album_list['data']['items']:
            username = album['user']['name']
            userid = album['user']['uid']
            userinfo = "用户名:"+username+"  用户id:"+str(userid)
            doc_id = str(album['item']['doc_id'])
            print("正在下载相册编号:"+doc_id)
            try:
                os.mkdir(doc_id)
            except Exception as e:
                print(e)
                continue
            with open(doc_id+"/info.txt","w",encoding='utf-8') as info:
                info.write(userinfo)
            pnum=0
            for photo in album['item']['pictures']:
                pnum+=1
                save_pic(doc_id,photo['img_src'],str(pnum)+".jpg")
            print(userinfo+"  》》》  下载"+str(pnum)+"张照片成功!\n")
    print ("结束时间:"+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
发表评论 / Comment

用心评论~

金玉良言 / Appraise
90后LV 1
2020-03-10 11:59
牛皮,我不知道怎么用,还没开始学Python,在B站看了你的视频就过来了,随即就发现了这个,请问计时器里还需要安装那些os、json、time等,是吗?
头像
90后LV 1
2020-03-10 12:00
@90后:解释器,不是计时器
头像
DYBOY站长已认证
2020-03-13 15:03
@90后:不需要
梵·苏打LV 1
2020-01-05 17:22
我就是想弱弱的问一下,我装完python后怎么运行这个源码,闪一下就没了...
头像
DYBOY站长已认证
2020-03-21 11:36
@梵·苏打:命令行下:python getMM.py
木殳LV 1
2019-06-23 20:01
怎么停止啊,根本停不下来
头像
DYBOY站长已认证
2019-06-28 23:38
@木殳:妹子那么多,停下来干嘛?
头像
梵·苏打LV 1
2020-01-05 18:09
@木殳:我知道,你看看你电脑主机后面有个黑色的线了吗,拔了再插上就行了!百试百灵!笔记本?!砸了吧!
江榕龄LV 1
2019-01-04 13:14
这个可以哦(´-ω-`)
头像
纯像SEOLV 1
2019-02-12 19:28
@江榕龄:写的不错了
头像
DYBOY站长已认证
2019-02-21 00:21
@纯像SEO:感谢支持

Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/blog.dyboy.cn/content/templates/dyblog/footer.php:56) in /www/wwwroot/blog.dyboy.cn/include/lib/view.php on line 23