Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make lyrics comments into cloud pictures by Python selenium

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I'll show you how Python selenium makes lyrics comments into word cloud pictures. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

Preface

A song is popular, and many people participate in the commentation. at this time, both good and bad comments come, and if there is no one to control the comments, they may be in a mess.

But I like to read comments, do not want to affect the good mood, want to see the wonderful comments, look at the lyrics, then how to do?

This time we will automatically download and save the lyrics to the computer and make a word cloud map for it to analyze and analyze.

This purpose

Download the lyrics comments automatically with selenium to make a good-looking word cloud picture.

The modules and packages used this time:

Re # regular expression built-in module

Selenium # to realize automatic operation of browser

Jieba # Chinese word Segmentation

Wordcloud # word cloud gallery

Imageio # Image Module

Time # built-in module

The installation method of the module to be installed:

Take selenium as an example, direct pip install selenium

If the download speed is slow, use the mirror source to download.

Driver installation

To achieve browser automation, we have to install a browser driver.

I won't post the URL. You can find it by searching the Google browser driver directly on the Internet. If you can't find it, talk to me in private.

It is recommended to use Google browser, take Google browser as an example, first take a look at the version of our browser.

There are three points in the upper right corner of the browser, click to open and click Settings.

Then click on Chrome, and the string of numbers on the right is the version number.

Then find the same version as your version number to download, if you don't have the same version, you can download the closest version.

If you put your code together, the disadvantage is that every time you use it, you have to download it if you don't save it.

Another way is to put it directly in your python directory, which has the advantage that you can use it many times at a time. The disadvantage is that every time the version is updated, you still have to download a new one.

Anyway, I always download new ones, and I don't use them very often.

First, download song reviews 1. Code implementation

First, import the module.

Module is something that must be imported. If it is not imported, it will report an error even if your code is correct at run time.

From selenium import webdriverimport re import time

Do not name the Python file name or package name selenium, which will cause it to fail to import.

Webdriver can be thought of as the driver of the browser, to drive the browser must use webdriver, support a variety of browsers.

Create a browser object

Driver = webdriver.Chrome ()

Request page

Driver.get ('https://music.163.com/#/song?id=569213220')driver.implicitly_wait(10) # implicit wait for browser to render page intelligent wait for driver.maximize_window () # maximize browser driver.switch_to.frame (0) # document.documentElement.scrollTop specify page height # document.documentElement.scrollHeight get page height # document.documentElement.scrollTop specify page height # Document.documentElement.scrollHeight gets the height of the page js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight'driver.execute_script (js)

Get comment data / save / click next page

For click in range (10): divs = driver.find_elements_by_css_selector ('.itm') for div in divs: cnt = div.find_element_by_css_selector ('.cnt.f-brk'). Text cnt = cnt.replace ('\ nbread,') # replace the newline character cnt = re.findall (': (. *), cnt) [0] with open ('contend.txt') Mode='a', encoding='utf-8') as f: f.write (cnt +'\ n') # find the next tab and click driver.find_element_by_css_selector ('.znxt'). Click () time.sleep (1) input ('program blocking.')

Finally exit the browser

Driver.quit () 2. Crawl the running effect of comments

Second, make word cloud map

Code implementation

Draw word cloud image / size setting, word cloud map pattern can choose what you like.

Import jieba # Chinese Thesaurus import wordcloud # Thesaurus import imageio # Image Module file = open ('contend.txt', mode='r', encoding='utf-8') txt = file.read () # print (txt) txt_list = jieba.lcut (txt) print (' segmentation result', txt_list) string = '.join (txt_list) print (' merge participle:' String) "" make word cloud image "" # read image img = imageio.imread ('music .png') # set word cloud image wc = wordcloud.WordCloud (width=1000, # word cloud map wide height=700, # picture high background_color= 'black', # word cloud image background color font_path='msyh.ttc', # word cloud font, Microsoft elegant black, system comes with scale=10 # font size # mask=img, stopwords=set ([line.strip () for line in open ('cn_stopwords.txt', mode='r', encoding='utf-8'). Readlines ()]) print (' drawing word cloud map') wc.generate (string) wc.to_file ('output2.png') print (' word cloud map made successfully')

Effect display

What are the five characteristics of python? what are the five characteristics of python: 1. It's easy to learn, and when you develop a program, you focus on solving problems, not understanding the language itself. two。 Object-oriented, compared with other major languages such as C++ and Java, Python implements object-oriented programming in a very powerful and simple way. 3. Portability, Python programs can run on a variety of platforms without modification. 4. Explanation, programs written in Python do not need to be compiled into binary code and can be run directly from the source code. 5. Open source, Python is one of FLOSS (Free / Open Source Software).

This is all about how Python selenium makes lyrics comments into word cloud diagrams. For more information about how Python selenium makes lyrics comments into word cloud maps, you can search the previous articles or browse the following articles to learn! I believe the editor will add more knowledge to you. I hope you can support it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report