selenium+phantomjs爬取bilibili 首先我們要下載phantomjs 你可以到 http://phantomjs.org/download.html 這裡去下載 下載完之後解壓到你想要放的位置 你需要配置一下環境變數哦 如下圖: 首先,我們怎麼讓瀏覽器模擬操作,也就是我們自己先 ...
selenium+phantomjs爬取bilibili
首先我們要下載phantomjs 你可以到 http://phantomjs.org/download.html 這裡去下載 下載完之後解壓到你想要放的位置 你需要配置一下環境變數哦
如下圖:
首先,我們怎麼讓瀏覽器模擬操作,也就是我們自己先分析好整個操作過程,哪個地方有什麼問題,把這些問題都提前測試好,沒問題了再進行寫代碼。
打開bilibili網站 https://www.bilibili.com/ 發現下圖登陸彈窗
那麼這裡我們就得先把這個彈窗去除,怎麼去呢?你刷新一下或者點一下 首頁 就不會出現了,所以這裡我們可以模擬再刷新一次或者點擊首頁。
接下來搜索關鍵詞 蔡徐坤 打球 這時就涉及到搜索輸入框和搜索按鈕
點擊搜索後我們看到了下列內容,其中圈起來的就是要爬的信息啦 這時就涉及到頁面源碼獲取,數據元素定位
那麼上面這個過程走完了的話 我們也可以選擇寫入xls格式,同時這裡還少了一個事,那就是我現在才爬了一頁,那難道不寫個自動化爬取全部嗎?
那此時就得解決迴圈獲取和寫入xls 更重要的事怎麼去操作頁數和下一頁按鈕
大致的思路就是這樣子了!!!
先導入這些模塊
from selenium import webdriver
from selenium.common.exceptions import TimeoutException #一條命令在足夠的時間內沒有完成則會拋出異常
from selenium.webdriver.common.by import By #支持的定位器分類
from selenium.webdriver.support.ui import WebDriverWait #等待頁面載入完成,找到某個條件發生後再繼續執行後續代碼,如果超過設置時間檢測不到則拋出異常
from selenium.webdriver.support import expected_conditions as EC #判斷元素是否載入
from bs4 import BeautifulSoup
import xlwt
定義一個瀏覽器對象並設置其他功能
browser = webdriver.Chrome() #初始化瀏覽器對象
WAIT = WebDriverWait(browser,10) #顯式等待,等待的時間是固定的,這裡為10秒 元素在指定時間內不可見就引發異常TimeoutException
browser.set_window_size(1400,900) #設置瀏覽器視窗大小
創建excel文件,再創建一張工作表,名為 蔡徐坤籃球,並且設置支持覆蓋原數據!
book=xlwt.Workbook(encoding='utf-8',style_compression=0) #創建excel文件,設置utf-8編碼,這樣就可以在excel中輸出中文了
sheet=book.add_sheet('蔡徐坤籃球',cell_overwrite_ok=True) #添加一張工作表 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,'發佈時間')
打開網站
browser.get('https://www.bilibili.com/')
尋找 “首頁” 元素
index = WAIT.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#primary_menu > ul > li.home > a')))
# 配合WebDriverWait類的until()方法進行靈活判斷 進行下一步操作
# 通過EC進行判斷某個元素中是否可見並且是enable的 這樣的話叫clickable(可點擊)
# 使用CSS選擇器選中頁面中的 首頁 進行點擊 目的為了第一次有個登錄彈窗 刷新就沒有 那就點擊下首頁來實現刷新
index.click() #點擊!
先判斷是否載入 輸入框 再判斷搜索按鈕是否能點擊 達到條件後輸入內容進行搜索
input = WAIT.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#banner_link > div > div > form > input'))) #判斷某個元素是否被加到DOM樹里,並不代表該元素一定可見(元素可以是隱藏的)
submit = WAIT.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="banner_link"]/div/div/form/button'))) #判斷搜索按鈕是否能點擊,這裡使用Xpath來尋找元素
input.send_keys('蔡徐坤 籃球') #用send_keys()方法進行搜索輸入框中輸入內容
submit.click() #點擊搜索!
這時搜索完 是彈出新的視窗 這時就得獲取視窗句柄 實現標簽頁跳轉
all_h = browser.window_handles #獲取所有視窗句柄
browser.switch_to.window(all_h[1]) #switch_to.window 標簽頁跳轉
接下來就是獲取頁面源碼了(此處非全部源碼)
WAIT.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#server-search-app > div.contain > div.body-contain > div > div.result-wrap.clearfix'))) #堅持是否載入完所有搜索結果
html = browser.page_source #page_source方法可以獲取到頁面源碼
然後搜索元素並提取內容進行保存
#遍歷所有搜索信息 並保存
list = soup.find(class_='all-contain').find_all(class_='info')
for item in list:
item_title = item.find('a').get('title')
item_link = item.find('a').get('href')
item_dec = item.find(class_='des hide').text
item_view = item.find(class_='so-icon watch-num').text
item_biubiu = item.find(class_='so-icon hide').text
item_date = item.find(class_='so-icon time').text
print('爬取:' + item_title)
再最後就是迴圈獲取每一頁提取數據最後寫入xls文件!!!
下麵就直接貼出代碼了
from selenium import webdriver
from selenium.common.exceptions import TimeoutException #一條命令在足夠的時間內沒有完成則會拋出異常
from selenium.webdriver.common.by import By #支持的定位器分類
from selenium.webdriver.support.ui import WebDriverWait #等待頁面載入完成,找到某個條件發生後再繼續執行後續代碼,如果超過設置時間檢測不到則拋出異常
from selenium.webdriver.support import expected_conditions as EC #判斷元素是否載入
from bs4 import BeautifulSoup
import xlwt
browser = webdriver.Chrome() #初始化瀏覽器對象
WAIT = WebDriverWait(browser,10) #顯式等待,等待的時間是固定的,這裡為10秒 元素在指定時間內不可見就引發異常TimeoutException
browser.set_window_size(1400,900) #設置瀏覽器視窗大小
book=xlwt.Workbook(encoding='utf-8',style_compression=0) #創建excel文件,設置utf-8編碼,這樣就可以在excel中輸出中文了
sheet=book.add_sheet('蔡徐坤籃球',cell_overwrite_ok=True) #添加一張工作表 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,'發佈時間')
n = 1
def seach():
try:
print('開始訪問b站....')
browser.get('https://www.bilibili.com/')
index = WAIT.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#primary_menu > ul > li.home > a')))
# 配合WebDriverWait類的until()方法進行靈活判斷 進行下一步操作
# 通過EC進行判斷某個元素中是否可見並且是enable的 這樣的話叫clickable(可點擊)
# 使用CSS選擇器選中頁面中的 首頁 進行點擊 目的為了第一次有個登錄彈窗 刷新就沒有 那就點擊下首頁來實現刷新
index.click() #點擊!
input = WAIT.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#banner_link > div > div > form > input'))) #判斷某個元素是否被加到DOM樹里,並不代表該元素一定可見(元素可以是隱藏的)
submit = WAIT.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="banner_link"]/div/div/form/button'))) #判斷搜索按鈕是否能點擊,這裡使用Xpath來尋找元素
input.send_keys('蔡徐坤 籃球') #用send_keys()方法進行搜索輸入框中輸入內容
submit.click() #點擊搜索!
print('跳轉到新視窗')
all_h = browser.window_handles #獲取所有視窗句柄
browser.switch_to.window(all_h[1]) #switch_to.window 標簽頁跳轉
get_source()
total = WAIT.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#server-search-app > div.contain > div.body-contain > div > div.page-wrap > div > ul > li.page-item.last > button"))) #等待載入後獲取所有頁數按鈕
return int(total.text) #返回頁碼數量
except TimeoutException:
return seach()
def get_source():
WAIT.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#server-search-app > div.contain > div.body-contain > div > div.result-wrap.clearfix'))) #堅持是否載入完所有搜索結果
html = browser.page_source #page_source方法可以獲取到頁面源碼
soup = BeautifulSoup(html,'lxml')
save_to_excel(soup)
def save_to_excel(soup):
#遍歷所有搜索信息 並保存
list = soup.find(class_='all-contain').find_all(class_='info')
for item in list:
item_title = item.find('a').get('title')
item_link = item.find('a').get('href')
item_dec = item.find(class_='des hide').text
item_view = item.find(class_='so-icon watch-num').text
item_biubiu = item.find(class_='so-icon hide').text
item_date = item.find(class_='so-icon time').text
print('爬取:' + item_title)
global n
sheet.write(n, 0, item_title)
sheet.write(n, 1, item_link)
sheet.write(n, 2, item_dec)
sheet.write(n, 3, item_view)
sheet.write(n, 4, item_biubiu)
sheet.write(n, 5, item_date)
n = n + 1
def next_page(page_num):
try:
print('獲取下一頁數據')
next_btn = WAIT.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#server-search-app > div.contain > div.body-contain > div > div.page-wrap > div > ul > li.page-item.next > button')))
#等待載入 下一頁 按鈕
next_btn.click() #點擊下一頁!
WAIT.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'#server-search-app > div.contain > div.body-contain > div > div.page-wrap > div > ul > li.page-item.active > button'),str(page_num)))
#判斷某個元素中的text是否包含了預期的字元串
get_source()
except TimeoutException:
browser.refresh() #刷新頁面
return next_page(page_num)
def main():
try:
total = seach()
for i in range(2,int(total+1)):
next_page(i)
finally:
browser.close()
browser.quit()
if __name__ == '__main__':
main()
book.save(u'蔡徐坤籃球.xls') #在字元串前加r,聲明為raw字元串,這樣就不會處理其中的轉義了。否則,可能會報錯