Selenium爬取b站主播頭像并以昵稱命名保存到本地
申明:資料來源于網(wǎng)絡(luò)及書本,通過理解、實踐、整理成學(xué)習(xí)筆記。
Pythion的Selenium自動化測試之獲取嗶哩嗶哩主播的頭像以昵稱命名保存到本地文件
效果圖
方法1
通過接口獲取
首先使用pip下載requests包
pip install requests
import requests # 通過接口獲取請求的接口:想要獲取網(wǎng)頁的url url = 'https://api.live.bilibili.com/xlive/web-interface/v1/second/getList?platform=web&parent_area_id=1&area_id=0&sort_type=sort_type_152&page=1' # 發(fā)送get請求,獲取返回數(shù)據(jù) request = requests.get(url) # 保存圖片的路徑 dir = '../requests/bilibili/' # 將獲取的數(shù)據(jù)轉(zhuǎn)化為json文件并獲取到圖片的鏈接 info = request.json()['data']['list'] for i in info: # 將圖片以主播的昵稱命名 file = open(dir + '{}.png'.format(i['uname']), 'wb') # 將圖片保存到之前的路徑 file.write(requests.get(i['face']).content) # 關(guān)閉文件流 file.close()
方法2
通過html定位獲取
首先使用pip下載requests和selenium包
pip install requests
pip install selenium
import requests from selenium import webdriver # 使用谷歌驅(qū)動打開谷歌瀏覽器 driver = webdriver.Chrome() # 訪問嗶哩嗶哩直播頁面 driver.get('https://live.bilibili.com/p/eden/area-tags?visit_id=2mwktlg4e2q0&areaId=0&parentAreaId=1') # 循環(huán)30次一次保存的頭像圖片 for i in range(1, 31): # xpth定位頭像的位置 image_xpath = '/html/body/div[1]/div[3]/div/ul/li[{}]/a/div[1]/div/div'.format(i) # 獲取位置的style屬性值 image_style_value = driver.find_element_by_xpath(image_xpath).get_attribute('style') # 從style屬性值中切片出圖片的鏈接 image_url = image_style_value[image_style_value.find('h'):image_style_value.find('@'):1] # xpath定位昵稱的位置 title_xpath = '/html/body/div[1]/div[3]/div/ul/li[{}]/a/div[2]/div[2]/div/span'.format(i) # 獲取位置的title值 name_title_value = driver.find_element_by_xpath(title_xpath).get_attribute('title') print(image_url) # 發(fā)送get請求,獲取返回數(shù)據(jù) request = requests.get(image_url) # 保存圖片的路徑 file = open('D:Python Projects/requests/bilibili/{}.jpg'.format(name_title_value), 'wb') # 將圖片保存到路徑 file.write(request.content) # 關(guān)閉文件流 file.close()
最后,在為大家增加一個獲取b站視頻信息的操作代碼
# coding:utf-8 import requests import json import time import pymysql import bs4 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36' } result = [] def get_aid(page): url = 'https://search.bilibili.com/all?keyword=爬蟲&from_source=nav_search&spm_id_from=333.851.b_696e7465726e6174696f6e616c486561646572.11' + '&page=' + str(page) response = requests.get(url, headers=headers, verify=False).text time.sleep(1) try: soup = bs4.BeautifulSoup(response, 'lxml').find('div', attrs={'id':'all-list'}).find('div', attrs={'class':'mixin-list'}) ul = soup.find('ul', attrs={'class':'video-list clearfix'}).find_all('li', attrs={'class':'video-item matrix'}) for item in ul: # print(item) info = item.find('div', attrs={'class': 'headline clearfix'}).find('span', attrs={'class': 'type avid'}).get_text() aid = info.replace('av', '') print(aid) result.append(aid) return result except: print('something is wrong') def get_contents(url): response = requests.get(url=url, headers=headers, verify=False).json() time.sleep(1) try: data_1 = response['data'] data = data_1['stat'] aid = data['aid'] view = data['view'] coin = data['coin'] like = data['like'] favorite = data['favorite'] share = data['share'] danmaku = data['danmaku'] print('視頻編號', aid) print('觀看數(shù)量', view) print('投幣數(shù)量', coin) print('收藏數(shù)量', favorite) print('點贊數(shù)量', like) print('分享數(shù)量', share) print('彈幕數(shù)量', danmaku) except: print('------------') if __name__ == '__main__': for i in range(1, 50): result = get_aid(i) for i in result: url = 'https://api.bilibili.com/x/web-interface/view?aid=' + str(i) get_contents(url)
到此這篇關(guān)于Selenium爬取b站主播頭像并以昵稱命名保存到本地的文章就介紹到這了,希望對大家有所幫助,更多相關(guān)python爬取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
python產(chǎn)生模擬數(shù)據(jù)faker庫的使用詳解
這篇文章主要介紹了python產(chǎn)生模擬數(shù)據(jù)faker庫的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11利用Python實現(xiàn)從PDF到CSV的轉(zhuǎn)換
將PDF轉(zhuǎn)換為CSV極大地提升了數(shù)據(jù)的實用價值,Python作為一種強大的編程語言,能夠高效完成這一轉(zhuǎn)換任務(wù),本文將介紹如何利用Python實現(xiàn)從PDF到CSV的轉(zhuǎn)換,需要的朋友可以參考下2024-07-07Python使用matplotlib實現(xiàn)的圖像讀取、切割裁剪功能示例
這篇文章主要介紹了Python使用matplotlib實現(xiàn)的圖像讀取、切割裁剪功能,結(jié)合實例形式分析了Python基于matplotlib操作圖片的加載、讀取、坐標(biāo)控制及裁剪相關(guān)操作技巧,需要的朋友可以參考下2018-04-04PyTorch中的神經(jīng)網(wǎng)絡(luò) Mnist 分類任務(wù)
這篇文章主要介紹了PyTorch中的神經(jīng)網(wǎng)絡(luò) Mnist 分類任務(wù),在本次的分類任務(wù)當(dāng)中,我們使用的數(shù)據(jù)集是 Mnist 數(shù)據(jù)集,這個數(shù)據(jù)集大家都比較熟悉,需要的朋友可以參考下2023-03-03淺談Python描述數(shù)據(jù)結(jié)構(gòu)之KMP篇
這篇文章主要介紹了Python描述數(shù)據(jù)結(jié)構(gòu)之KMP篇,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09