前言
嗨嘍,大家好(o゚v゚)ノ 這里是魔王

[課 題]:
Python爬取某站視頻彈幕或者騰訊視頻彈幕,繪制詞云圖
[知識點]:
1. 爬蟲基本流程
2. 正則
3. requests >>> pip install requests
4. jieba >>> pip install jieba
5. imageio >>> pip install imageio
6. wordcloud >>> pip install wordcloud
[開發環境]:
Python 3.8
Pycharm
win + R 輸入cmd 輸入安裝命令 pip install 模塊名 如果出現爆紅 可能是因為 網路連接超時 切換國內鏡像源
相對應的安裝包/安裝教程/激活碼/使用教程/學習資料/工具插件 可以找我
爬取彈幕
爬蟲基本思路流程:
一. 資料來源分析
1. 確定我們想要資料是什么?
爬取某站彈幕資料 保存文本txt
2. 通過開發者工具進行抓包分析...
通過 介面可以直接找到視頻的彈幕資料地址
二. 爬蟲代碼實作步驟:
1. 發送請求, 對于(評論看) 發送請求
需要注意點:
- 請求方式確定
- 請求頭引數
2. 獲取資料, 獲取服務器回傳的資料
3. 決議資料, 提取我們想要資料內容, 彈幕資料
4. 保存資料, 把獲取下來的資料內容保存txt文本
模擬瀏覽器對于服務器發送請求
“”"
匯入模塊
import requests # 資料請求模塊 第三方模塊 pip install requests
import re # 正則運算式模塊 內置模塊 不需要安裝

代碼
# # 1. 發送請求
# url = '(評論看)'
# # headers 請求頭 作用把Python代碼進行偽裝, 模擬成瀏覽器去發送請求
# # user-agent 瀏覽器基本身份標識
# # headers 請求頭 字典資料型別
# headers = {
# 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'
# }
# # 通過requests模塊里面get請求方法, 對于url地址發送請求, 并且攜帶上headers請求頭, 最后用response變數去接識訓傳資料
# response = requests.get(url=url, headers=headers)
# response.encoding = response.apparent_encoding
# # <Response [200]> response物件 200狀態碼 表示請求成功
# # 如果你想要獲取 網頁源代碼一樣的資料內容的話, 是獲取回應體的文本資料
# # 如果服務器回傳的資料, 不是完整json資料 字典資料 直接獲取response.json()就會報錯
# # 2. 獲取資料 response.text 回傳資料 html字串資料
# # print(response.text)
# # 3. 決議資料, 決議方式 re[可以直接對于字串資料進行提取] css xpath [主要根據標簽屬性/節點提取資料]
# # () 精確匹配 表示想要的資料 泛匹配 .*? 正則運算式元字符 可以匹配任意字符(除了換行符\n以外)
# data_list = re.findall('<d p=".*?">(.*?)</d>', response.text)
# for index in data_list:
# # mode 保存方式 encoding 編碼
# # pprint.pprint() 格式化輸入 json字典資料
# with open('彈幕.txt', mode='a', encoding='utf-8') as f:
# f.write(index)
# f.write('\n')
# print(index)
url = 'https://mapi.vip.com/vips-mobile/rest/shopping/pc/search/product/rank?callback=getMerchandiseIds&app_name=shop_pc&app_version=4.0&warehouse=VIP_NH&fdc_area_id=104104101&client=pc&mobile_platform=1&province_id=104104&api_key=70f71280d5d547b2a7bb370a529aeea1&user_id=&mars_cid=1634797375792_17a23bdc351b36f2915c2f7ec16dc88e&wap_consumer=a&standby_id=nature&keyword=%E5%8F%A3%E7%BA%A2&lv3CatIds=&lv2CatIds=&lv1CatIds=&brandStoreSns=&props=&priceMin=&priceMax=&vipService=&sort=0&pageOffset=0&channelId=1&gPlatform=PC&batchSize=120&_=1639640088314'
headers = {
'referer': 'https://category.vip.com/',
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
print(response.text)

制作詞云圖

[知識點]:
1. 爬蟲基本流程
2. 正則
3. requests >>> pip install requests
4. jieba >>> pip install jieba
5. imageio >>> pip install imageio
6. wordcloud >>> pip install wordcloud
[開發環境]:
Python 3.8
Pycharm
匯入模塊
import jieba # 結巴分詞 pip install jieba
import wordcloud # 詞云圖 pip install wordcloud
import imageio # 讀取本地圖片 修改詞云圖形
img = imageio.imread('蘋果.png')
1. 讀取彈幕資料
f = open('彈幕.txt', encoding='utf-8')
text = f.read()
# print(text)

2. 分詞, 把一句話 分割成很多詞匯
text_list = jieba.lcut(text)
print(text_list)
# 串列轉成字串
text_str = ' '.join(text_list)
print(text_str)
3. 詞云圖配置
wc = wordcloud.WordCloud(
width=500, # 寬度
height=500, # 高度
background_color='white', # 背景顏色
mask=img,
stopwords={'每', '一個', '了', '的', '夢想', '助力'},
font_path='msyh.ttc' # 字體檔案
)
wc.generate(text_str)
wc.to_file('詞云1.png')


好了,我的這篇文章寫到這里就結束啦!
有更多建議或問題可以評論區或私信我哦!一起加油努力叭(? ?_?)?
喜歡就關注一下博主,或點贊收藏評論一下我的文章叭!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/382986.html
標籤:其他
上一篇:R-FCN:走向全卷積的網路
