Python爬虫入门教程 12-100 半次元COS图爬取

半次元COS图爬取-写在前面

今天在浏览网站的时候,突然一个莫名的连接指引着我跳转到了半次元网站 https://bcy.net/ 打开以后,发现也没有什么有意思的内容,职业的敏感让我瞬间联想到了 cosplay ,这种网站必然会有这个的存在啊,因而乎,我准备好个人大爬虫了。
python



在这里插入图片描述

把上面的连接打开以后,被我发现了吧,就知道个人第八感不错滴。接下来就是找入口,必定要找到图片连接的入口才能够作下面的操做
在这里插入图片描述mongodb

这个页面不断往下拖拽,页面会一直加载,当时当你拖拽一会,就停下来了,就是这个时机
在这里插入图片描述数据库

发现入口,在我实际的操做中,其实还发现了不少其余的入口,这个就不一一的解释了,赶忙上车,进入 view more 以后,发现了页面依旧是一个下拉刷新的布局方式,专业术语 瀑布流json

半次元COS图爬取-python爬虫第一步

打开开发者工具,切换到network以后,发现 不少xhr请求,发现这个,就表明这个网站很容易爬取了数组

在这里插入图片描述
提取待爬取的连接,分析规律多线程

https://bcy.net/circle/timeline/loadtag?since=0&grid_type=timeline&tag_id=1482&sort=hot
https://bcy.net/circle/timeline/loadtag?since=26499.779&grid_type=timeline&tag_id=1482&sort=hot
https://bcy.net/circle/timeline/loadtag?since=26497.945&grid_type=timeline&tag_id=1482&sort=hot

发现只有一个参数在变,并且这变化好像没有任何规律能够寻找,没事,看数据,你就能够发现其中的奥妙了app

在这里插入图片描述
这个网站的原理很简单,就是经过不断获取每次数据的最后一条的since而后获取接下来的数据,那么咱们按照它的规律实现代码就能够了,不要多线程了,这种规律是没有办法进行实操的。
此次的数据我把它存储到mongodb里面,由于没有办法一次所有获取到,因此可能须要下次在继续使用python爬虫

if __name__ == '__main__':
    ###  mongodb 的一些基本操做   
    DATABASE_IP = '127.0.0.1'
    DATABASE_PORT = 27017
    DATABASE_NAME = 'sun'
    start_url = "https://bcy.net/circle/timeline/loadtag?since={}&grid_type=timeline&tag_id=399&sort=recent"
    client = MongoClient(DATABASE_IP, DATABASE_PORT)

    db = client.sun
    db.authenticate("dba", "dba")
    collection  =  db.bcy  # 准备插入数据
    #####################################3333
    get_data(start_url,collection)

获取网页数据这个地方,由咱们前面的经验就变得很简单了函数

## 半次元COS图爬取-获取数据函数  
def get_data(start_url,collection):
    since = 0
    while 1:
        try:
            with requests.Session() as s:
                response = s.get(start_url.format(str(since)),headers=headers,timeout=3)
                res_data = response.json()
                if res_data["status"] == 1:
                    data = res_data["data"]  # 获取Data数组
                    time.sleep(0.5)
                ## 数据处理
                since = data[-1]["since"]  # 获取20条数据的最后一条json数据中的since
                ret = json_handle(data)   # 代码实如今下面
                try:
                    print(ret)
                    collection.insert_many(ret)   # 批量出入数据库
                    print("上述数据插入成功!!!!!!!!")
                except Exception as e:
                    print("插入失败")
                    print(ret)

                ##
        except Exception as e:
            print("!",end="异常,请注意")
            print(e,end=" ")
    else:
        print("循环完毕")

网页解析代码工具

# 对JSON数据进行处理
def json_handle(data):
    # 提取关键数据
    list_infos = []
    for item in data:
        item = item["item_detail"]
        try:
            avatar = item["avatar"] # 用户头像
            item_id = item["item_id"] # 图片详情页面
            like_count = item["like_count"] # 喜欢数目
            pic_num = item["pic_num"] if "pic_num" in item else 0 # 图片总数
            reply_count =item["reply_count"]
            share_count =item["share_count"]
            uid = item["uid"]
            plain = item["plain"]
            uname = item["uname"]
            list_infos.append({"avatar":avatar,
                               "item_id":item_id,
                               "like_count":like_count,
                               "pic_num":pic_num,
                               "reply_count":reply_count,
                               "share_count":share_count,
                               "uid":uid,
                               "plain":plain,
                               "uname":uname})
        except Exception as e:
            print(e)
            continue
        return list_infos

到如今就实现了,代码跑起来
在这里插入图片描述

想要源码的,在评论里面留言本身的邮箱或者其余任何我能联系到你的方式均可以哒。


相关文章
相关标签/搜索