python小練習之爬魷魚游戲的評價生成詞云
前言
魷魚游戲是什么,相信大家都不陌生了,雖然說博主沒看過這部劇,但是還是對豆瓣的評論有點好奇,剛剛好近期學習了selenium,就當練練手了,來吧來吧,爬爬爬。
分析頁面
還是老樣子,兄弟們先打開我們最喜歡的google瀏覽器,點擊F12,開啟爬蟲快樂模式
來到頁面,如下圖步驟,逐個點擊
然后我們就發(fā)現(xiàn)這個頁面確實很簡單,每一條評論就是包在了class為short的span標簽內(nèi),那就可以開始寫xpath了,如下圖
這樣一頁的評論就拿到了,接下來就是換頁了
有一個小技巧,不需要我們自己寫xpath,直接用google瀏覽器可以生成xpath,如下圖所示
點擊這個Copy path這樣就拿到了按鈕的xpath的內(nèi)容,然后實現(xiàn)點擊頁面就可以了,好了就這樣分析完了,接下來開始寫代碼了。
重要代碼
selenium打開豆瓣短評頁面
# 待打開的頁面 url = 'https://movie.douban.com/subject/34812928/comments?limit=20&status=P&sort=new_score' # 躲避智能檢測 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來獲取評論內(nèi)容
這里獲取評論的xpath語句
//span[@class="short"]
獲取評論代碼
options = driver.find_elements(By.XPATH, '//span[@class="short"]') for i in options: text=text+i.text
實現(xiàn)跳轉(zhuǎn)下一頁
下一頁的按鈕xpath
//*[@id="paginator"]/a
跳轉(zhuǎ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) # 分詞后在單獨個體之間加上空格 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()
評論獲取代碼
# -*- 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' # 躲避智能檢測 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='' # 獲取所有的選項元素 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)
成果
最后爬取的評論生成了詞云圖,如下圖所示
就這樣就結(jié)束了,還是很簡單的
到此這篇關于python小練習之爬魷魚游戲的評價生成詞云的文章就介紹到這了,更多相關Python 爬取魷魚游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python-OpenCV中的cv2.inpaint()函數(shù)的使用
大多數(shù)人會在家里放一些舊的退化照片,上面有一些黑點,一些筆畫等。你有沒有想過恢復它?本文就來介紹一下方法,感興趣的可以了解一下2021-06-06Python pyinotify日志監(jiān)控系統(tǒng)處理日志的方法
這篇文章主要介紹了Python pyinotify日志監(jiān)控系統(tǒng)處理日志的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03詳解Python如何實現(xiàn)壓縮與解壓縮數(shù)據(jù)
當數(shù)據(jù)量大的時候,自然而然想到的就是對數(shù)據(jù)進行壓縮,這篇文章主要為大家介紹了Python可以實現(xiàn)壓縮與解壓縮數(shù)據(jù)的相關模塊的使用,希望對大家有所幫助2024-02-02