1. 程式人生 > >Python爬蟲入門教程 12-100 半次元COS圖爬取

Python爬蟲入門教程 12-100 半次元COS圖爬取

寫在前面

今天在瀏覽網站的時候,忽然一個莫名的連結指引著我跳轉到了半次元網站 https://bcy.net/ 開啟之後,發現也沒有什麼有意思的內容,職業的敏感讓我瞬間聯想到了 cosplay ,這種網站必然會有這個的存在啊,於是乎,我準備好我的大爬蟲了。



在這裡插入圖片描述

把上面的連結開啟之後,被我發現了吧,就知道我的第八感不錯滴。接下來就是找入口,一定要找到圖片連結的入口才可以做下面的操作
在這裡插入圖片描述

這個頁面不斷往下拖拽,頁面會一直載入,當時當你拖拽一會,就停下來了,就是這個時機
在這裡插入圖片描述

發現入口,在我實際的操作中,其實還發現了很多其他的入口,這個就不一一的解釋了,趕緊上車,進入 view more

之後,發現了頁面依舊是一個下拉重新整理的佈局方式,專業術語 瀑布流

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

發現只有一個引數在變,而且這變化好像沒有任何規律可以尋找,沒事,看資料,你就可以發現其中的奧妙了

在這裡插入圖片描述
這個網站的原理很簡單,就是通過不斷獲取每次資料的最後一條的since然後獲取接下來的資料,那麼我們按照它的規律實現程式碼就可以了,不要多執行緒了,這種規律是沒有辦法進行實操的。
這次的資料我把它儲存到mongodb裡面,因為沒有辦法一次全部獲取到,所以可能需要下次在繼續使用

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)

獲取網頁資料這個地方,由我們前面的經驗就變得很簡單了

# 獲取資料函式  
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

到現在就實現了,程式碼跑起來
在這裡插入圖片描述

想要原始碼的,在評論裡面留言自己的郵箱或者其他任何我能聯絡到你的方式都可以噠。