前言
魷魚游戲是什么,相信大家都不陌生了,雖然說博主沒看過這部劇,但是還是對(duì)豆瓣的評(píng)論有點(diǎn)好奇,剛剛好近期學(xué)習(xí)了selenium,就當(dāng)練練手了,來吧來吧,爬爬爬。
分析頁面
還是老樣子,兄弟們先打開我們最喜歡的google瀏覽器,點(diǎn)擊F12,開啟爬蟲快樂模式
來到頁面,如下圖步驟,逐個(gè)點(diǎn)擊
然后我們就發(fā)現(xiàn)這個(gè)頁面確實(shí)很簡單,每一條評(píng)論就是包在了class為short的span標(biāo)簽內(nèi),那就可以開始寫xpath了,如下圖
這樣一頁的評(píng)論就拿到了,接下來就是換頁了
有一個(gè)小技巧,不需要我們自己寫xpath,直接用google瀏覽器可以生成xpath,如下圖所示
點(diǎn)擊這個(gè)Copy path這樣就拿到了按鈕的xpath的內(nèi)容,然后實(shí)現(xiàn)點(diǎn)擊頁面就可以了,好了就這樣分析完了,接下來開始寫代碼了。
重要代碼
selenium打開豆瓣短評(píng)頁面
# 待打開的頁面 url = 'https://movie.douban.com/subject/34812928/comments?limit=20&status=P&sort=new_score' # 躲避智能檢測(cè) option = webdriver.ChromeOptions() # option.headless = True option.add_experimental_option('excludeSwitches', ['enable-automation']) option.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=option) driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})' }) #打開頁面 driver.get(url)
根據(jù)xpath來獲取評(píng)論內(nèi)容
這里獲取評(píng)論的xpath語句
//span[@class="short"]
獲取評(píng)論代碼
options = driver.find_elements(By.XPATH, '//span[@class="short"]') for i in options: text=text+i.text
實(shí)現(xiàn)跳轉(zhuǎn)下一頁
下一頁的按鈕xpath
//*[@id="paginator"]/a
跳轉(zhuǎn)按鈕點(diǎn)擊代碼
nextpage = driver.find_element(By.XPATH, '//*[@id="paginator"]/a') nextpage.click()
完整代碼
詞云生成工具類
# -*- codeing = utf-8 -*- # @Time : 2021/10/9 20:54 # @Author : xiaow # @File : wordcloudutil.py # @Software : PyCharm from wordcloud import WordCloud import PIL.Image as image import numpy as np import jieba def trans_CN(text): # 接收分詞的字符串 word_list = jieba.cut(text) # 分詞后在單獨(dú)個(gè)體之間加上空格 result = " ".join(word_list) return result def getWordCloud(text): # print(text) text = trans_CN(text) # 詞云背景圖 mask = np.array(image.open("E://file//pics//mask3.jpg")) wordcloud = WordCloud( mask=mask, # 字體樣式文件 font_path="C:\Windows\Fonts\STXINGKA.TTF", background_color='white' ).generate(text) image_produce = wordcloud.to_image() image_produce.show()
評(píng)論獲取代碼
# -*- codeing = utf-8 -*- # @Time : 2021/6/27 22:29 # @Author : xiaow # @File : test.py # @Software : PyCharm import time from selenium import webdriver from selenium.webdriver.common.by import By from api import wordcloudutil if __name__ == '__main__': url = 'https://movie.douban.com/subject/34812928/comments?limit=20&status=P&sort=new_score' # 躲避智能檢測(cè) option = webdriver.ChromeOptions() # option.headless = True option.add_experimental_option('excludeSwitches', ['enable-automation']) option.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=option) driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})' }) driver.get(url) text='' # 獲取所有的選項(xiàng)元素 j=0 while 1: # 定位到新跳轉(zhuǎn)的頁面 time.sleep(1) driver.switch_to.window(driver.window_handles[0]) options = driver.find_elements(By.XPATH, '//span[@class="short"]') for i in options: text=text+i.text time.sleep(2) nextpage = driver.find_element(By.XPATH, '//*[@id="paginator"]/a') nextpage.click() j=j+1 if j>10: break print(text) wordcloudutil.getWordCloud(text)
成果
最后爬取的評(píng)論生成了詞云圖,如下圖所示
就這樣就結(jié)束了,還是很簡單的
到此這篇關(guān)于python小練習(xí)之爬魷魚游戲的評(píng)價(jià)生成詞云的文章就介紹到這了,更多相關(guān)Python 爬取魷魚游戲內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_43627076/article/details/120817996