使用selenium+phantomjs爬取B站關於小米10的視頻
目標
b站搜索頁面https://search.bilibili.com/,爬取關於小米10的視頻
分析
搜索小米10後的結果
在這個頁面可以看到,我們需要的數據有
名稱
視頻地址
描述
觀看次數
彈幕數量
發佈時間
待會我們就把這些數據都爬下來
代碼
# 爬取嗶哩嗶哩的數據
# 導入 web 驅動模塊
import xlwt
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
# 創建一個 Chrome 驅動
browser = webdriver.PhantomJS()
# browser = webdriver.Chrome()
WAIT = WebDriverWait(browser, 10)
# 全局變量:視頻總頁數
total_page = 0
# 創建excel文件
book = xlwt.Workbook(encoding='utf-8', style_compression=0)
sheet = book.add_sheet('小米10視頻', cell_overwrite_ok=True)
sheet.write(0, 0, '名稱')
sheet.write(0, 1, '時長')
sheet.write(0, 2, '地址')
sheet.write(0, 3, '描述')
sheet.write(0, 4, '觀看次數')
sheet.write(0, 5, '彈幕數')
sheet.write(0, 6, '發佈時間')
# 全局變量:控制excel中的行
row = 0
# 打開瀏覽器搜索
def search():
# 使用 get 方法打開網頁
url = 'https://search.bilibili.com/'
browser.get(url)
# 獲取到輸入框
input = browser.find_element_by_css_selector('#search-keyword')
# 獲取到輸入框之後我們就往裏面寫入我們要搜索的內容
input.send_keys('小米10')
# 獲取到搜索按鈕
btn = browser.find_element_by_class_name('searchBtn')
# 按鈕點擊
btn.click()
# 等到這個元素可操作的時候纔會繼續執行下一步
total = WAIT.until(EC.presence_of_element_located(
(By.CSS_SELECTOR, '#all-list > div.flow-loader > div.page-wrap > div > ul > li.page-item.last > button')))
print(total.text)
global total_page
total_page = int(total.text)
# 獲取網頁源碼
def get_source():
html = browser.page_source
soup = BeautifulSoup(html, 'lxml')
parse_html(soup)
# 解析html
def parse_html(soup):
list = soup.find(class_='video-list clearfix').find_all('li')
global row
for item in list:
row += 1
item_title = item.find('a').get('title')
item_link = item.find('a').get('href')
item_duration = item.find(class_='so-imgTag_rb').text
item_dec = item.find(class_='des hide').text
item_watch_num = item.find(class_='so-icon watch-num').text
item_danmu = item.find(class_="so-icon hide").text
item_up_time = item.find(class_='so-icon time').text
# 將數據寫入excel
sheet.write(row, 0, item_title)
sheet.write(row, 1, item_duration)
sheet.write(row, 2, item_link)
sheet.write(row, 3, item_dec.strip())
sheet.write(row, 4, item_watch_num)
sheet.write(row, 5, item_danmu)
sheet.write(row, 6, item_up_time)
result = '爬取結果:' + item_title\
+ item_duration \
+ item_dec \
+ item_watch_num \
+ item_danmu \
+ item_up_time + '\n'
print(result)
# print(list)
# 下一頁
def next_page(page_num):
try:
btn_next_page = WAIT.until(
EC.element_to_be_clickable((
By.CSS_SELECTOR, '#all-list > div.flow-loader > div.page-wrap > div > ul > li.page-item.next > button')))
btn_next_page.click()
WAIT.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,
'#all-list > div.flow-loader > div.page-wrap > div > ul > li.page-item.active > button'),
str(page_num)))
get_source()
except TimeoutError:
browser.refresh()
return next_page(page_num)
# 開始爬取數據
def start():
try:
search()
for x in range(2, int(total_page + 1)):
next_page(x)
finally:
browser.close()
if __name__ == '__main__':
# 開始爬取
start()
# 保存文件
book.save(u'B站小米10視頻.xlsx')