知識點
- 爬蟲基本流程
- 正則
- requests >>> pip install requests
- jieba >>> pip install jieba
- imageio >>> pip install imageio
- wordcloud >>> pip install wordcloud
開發環境
- add path 勾選 其他可以默認安裝
- Python越新的版本 代表的一些模塊不太兼容
- Python 3.6 / 3.8 >>> python解釋器(環境)
- Pycharm >>> python編輯器
【付費VIP完整版】只要看了就能學會的教程,80集Python基礎入門視頻教學
點這里即可免費在線觀看
代碼實作程序步驟:
- 匯入模塊
- 發送請求 對于 彈幕url發送請求
- 決議資料 提取我們想要彈幕內容
- 保存資料 爬取彈幕 可以保存csv檔案 保存txt
爬蟲代碼
匯入模塊
import requests
import re
發送請求
url = 'https://api.bilibili.com/x/v1/dm/list.so?oid=392402545'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
response.encoding = response.apparent_encoding
決議資料
# re 正則運算式
html_data = re.findall('<d p=".*?">(.*?)</d>', response.text)
print(html_data)
保存資料
for index in html_data:
with open('彈幕1.txt', mode='a', encoding='utf-8') as f:
f.write(index)
f.write('\n')
print(index)

詞云代碼
import jieba # 分詞模塊 pip install jiebe
import wordcloud # 詞云模塊 pip install wordcloud
import imageio # 自定義詞云樣式 pip install imageio
py = imageio.imread('python.png')
# 詞云 統計哪些詞語出現次數比較多, 次數出現的越多的話 字體顯示越大
f = open('彈幕1.txt', encoding='utf-8')
txt = f.read()
# print(txt)
txt_list = jieba.lcut(txt)
string = ' '.join(txt_list)
print(string)
wc = wordcloud.WordCloud(
width=500, # 寬度
height=500, # 高度
background_color='white', # 背景顏色
font_path='msyh.ttc', # 字體檔案
mask=py,
stopwords={'了', '這個', '啊', '我', '的'}, # 停用詞
# contour_width=5,
# contour_color='red'
)
wc.generate(string)
wc.to_file('output3.png')


對于本篇文章有疑問,或者想要資料集的同學也可以點這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/306241.html
標籤:python
上一篇:Python?30行代碼?爬取王者榮耀所有英雄皮膚圖片?
下一篇:OpenCV——高斯濾波
