Python實(shí)現(xiàn)微博動(dòng)態(tài)圖片爬取詳解
由于微博的網(wǎng)頁(yè)端有反爬蟲,需要登錄,所以我們換個(gè)思路,曲線救國(guó)。
我們找到微博在瀏覽器上面用于手機(jī)端的調(diào)試的APL,如何找到呢?
我這邊直接附上微博的手機(jī)端的地址:https://m.weibo.cn/
1.模擬搜索用戶
搜索一個(gè)用戶獲取到的api:
https://m.weibo.cn/api/container/getIndex?containerid=100103type=1&q=半半子&page_type=searchall
1.1 對(duì)api內(nèi)參數(shù)進(jìn)行處理
containerid=100103type=1&q=半半子 ——> containerid=100103type%3D1%26q%3D%E5%8D%8A%E5%8D%8A%E5%AD%90_
這個(gè)參數(shù)需要提前轉(zhuǎn)碼,否則無法獲取數(shù)據(jù)
1.2 對(duì)用戶名進(jìn)行判斷,通過后提取uid
2.獲取more參數(shù)
GET
api : https://m.weibo.cn/profile/info?uid=2830125342
2.1 提取并處理more參數(shù)
3.循環(huán)提取圖片id
GET
api : https://m.weibo.cn/api/container/getIndex?containerid=2304132830125342_-_WEIBO_SECOND_PROFILE_WEIBO&page_type=03&page=1
3.1 提取圖片id——>pic_id
3.2 獲取發(fā)送圖片用戶
3.3 根據(jù)動(dòng)態(tài)創(chuàng)建時(shí)間生成用戶唯一識(shí)別碼
4.下載圖片
我們從瀏覽器抓包中就會(huì)獲取到后臺(tái)服務(wù)器發(fā)給瀏覽器的圖片鏈接
https://wx2.sinaimg.cn/large/pic_id.jpg
瀏覽器打開這個(gè)鏈接就可以直接下載圖片
爬取完整代碼:
import os import sys import time from urllib.parse import quote import requests headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36' } def time_to_str(c_at): ti = time.strptime(c_at, '%a %b %d %H:%M:%S +0800 %Y') time_str = time.strftime('%Y%m%d%H%M%S', ti) return time_str # 1. 搜索用戶,獲取uid # 2. 通過uid獲取空間動(dòng)態(tài)關(guān)鍵參數(shù) # 3. 獲取動(dòng)態(tài)內(nèi)容 # 4. 提取圖片參數(shù) # 5. 下載圖片 # 1. 搜索用戶,獲取uid # ========= 用戶名 ========= # 輸入不同的用戶名可切換下載的用戶圖片 # 用戶名需要完全匹配 name = '半半子_' # ========================= con_id = f'100103type=1&q={name}' # 這個(gè)條件需要轉(zhuǎn)碼 con_id = quote(con_id, 'utf-8') user_url = f'https://m.weibo.cn/api/container/getIndex?containerid={con_id}&page_type=searchall' user_json = requests.get(url=user_url, headers=headers).json() user_cards = user_json['data']['cards'] for card_num in range(len(user_cards)): if 'mblog' in user_cards[card_num]: if user_cards[card_num]['mblog']['user']['screen_name'] == name: print(f'正在獲取{name}的空間') # 2. 通過uid獲取空間動(dòng)態(tài)關(guān)鍵參數(shù) user_id = user_cards[card_num]['mblog']['user']['id'] info_url = f'https://m.weibo.cn/profile/info?uid={user_id}' info_json = requests.get(url=info_url, headers=headers).json() more_card = info_json['data']['more'].split("/")[-1] break file_name = 'weibo' if not os.path.exists(file_name): os.mkdir(file_name) if len(more_card) == 0: sys.exit() page_type = '03' page = 0 while True: # 3. 獲取動(dòng)態(tài)內(nèi)容 page += 1 url = f'https://m.weibo.cn/api/container/getIndex?containerid={more_card}&page_type={page_type}&page={page}' param = requests.get(url=url, headers=headers).json() cards = param['data']['cards'] print(f'第 {page} 頁(yè)') for i in range(len(cards)): card = cards[i] if card['card_type'] != 9: continue mb_log = card['mblog'] # 4. 提取圖片參數(shù) # 獲取本人的圖片 pic_ids = mb_log['pic_ids'] user_name = mb_log['user']['screen_name'] created_at = mb_log['created_at'] if len(pic_ids) == 0: # 獲取轉(zhuǎn)發(fā)的圖片 if 'retweeted_status' not in mb_log: continue if 'pic_ids' not in mb_log['retweeted_status']: continue pic_ids = mb_log['retweeted_status']['pic_ids'] user_name = mb_log['retweeted_status']['user']['screen_name'] created_at = mb_log['retweeted_status']['created_at'] time_name = time_to_str(created_at) pic_num = 1 print(f'======== {user_name} ========') # 5. 下載圖片 for pic_id in pic_ids: pic_url = f'https://wx2.sinaimg.cn/large/{pic_id}.jpg' pic_data = requests.get(pic_url, headers) # 文件名 用戶名_日期(年月日時(shí)分秒)_編號(hào).jpg # 例:半半子__20220212120146_1.jpg with open(f'{file_name}/{user_name}_{time_name}_{pic_num}.jpg', mode='wb') as f: f.write(pic_data.content) print(f' 正在下載:{pic_id}.jpg') pic_num += 1 time.sleep(2)
到此這篇關(guān)于Python實(shí)現(xiàn)微博動(dòng)態(tài)圖片爬取詳解的文章就介紹到這了,更多相關(guān)Python微博圖片爬取內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
手把手教你利用opencv實(shí)現(xiàn)人臉識(shí)別功能(附源碼+文檔)
最近搞一個(gè)人臉識(shí)別的項(xiàng)目練練手,不得不感嘆opencv做人臉檢測(cè)實(shí)在是強(qiáng),這篇文章主要給大家介紹了關(guān)于利用opencv實(shí)現(xiàn)人臉識(shí)別功能的相關(guān)資料,并附上了源碼以及文檔,需要的朋友可以參考下2021-09-09python內(nèi)置模塊collections知識(shí)點(diǎn)總結(jié)
這篇文章主要介紹了python內(nèi)置模塊collections知識(shí)點(diǎn)總結(jié),有興趣的朋友們學(xué)習(xí)下。2019-12-12高效測(cè)試用例組織算法pairwise之Python實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄咝y(cè)試用例組織算法pairwise之Python實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07解決pip install的時(shí)候報(bào)錯(cuò)timed out的問題
今天小編就為大家分享一篇解決pip install的時(shí)候報(bào)錯(cuò)timed out的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06python神經(jīng)網(wǎng)絡(luò)tensorflow利用訓(xùn)練好的模型進(jìn)行預(yù)測(cè)
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)tensorflow利用訓(xùn)練好的模型進(jìn)行預(yù)測(cè),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05selenium設(shè)置proxy、headers的方法(phantomjs、Chrome、Firefox)
這篇文章主要介紹了selenium設(shè)置proxy、headers的方法(phantomjs、Chrome、Firefox),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11