網(wǎng)站開發(fā)角色分配權(quán)限百度明星人氣榜入口
嗨嘍~大家好呀,這里是魔王吶 ? ~!
python更多源碼/資料/解答/教程等 點擊此處跳轉(zhuǎn)文末名片免費獲取
環(huán)境使用:
-
Python 3.10
-
Pycharm
第三方模塊使用:
-
import requests >>> pip install requests
-
import wordcloud >>> pip install wordcloud
-
import jieba >>> pip install jieba
爬蟲基本流程:
一. 數(shù)據(jù)來源分析
1. 明確需求: 明確采集的網(wǎng)站以及數(shù)據(jù)內(nèi)容- 網(wǎng)址: https://weibo.com/2803301701/NxcPMvW2l- 數(shù)據(jù): 評論內(nèi)容
2. 抓包分析: 通過開發(fā)者工具進行抓包- 打開開發(fā)者工具: F12- 刷新網(wǎng)頁- 通過關(guān)鍵字查找對應的數(shù)據(jù)關(guān)鍵字: 評論的內(nèi)容數(shù)據(jù)包地址: https://weibo.com/ajax/statuses/buildComments?is_reload=1&id=4979141627611265&is_show_bulletin=2&is_mix=0&count=10&uid=2803301701&fetch_level=0&locale=zh-CN
二. 代碼實現(xiàn)步驟
1. 發(fā)送請求 -> 模擬瀏覽器對于url地址發(fā)送請求
2. 獲取數(shù)據(jù) -> 獲取服務器返回響應數(shù)據(jù)
3. 解析數(shù)據(jù) -> 提取評論內(nèi)容
4. 保存數(shù)據(jù) -> 保存本地文件 (文本 csv Excel 數(shù)據(jù)庫)
多頁數(shù)據(jù)采集: 分析請求鏈接變化規(guī)律(主要看請求參數(shù))
翻頁: 點擊下一頁 / 滑動
flow: 0 多了這個參數(shù)
max_id: 第一頁是沒有 第二/三頁一串數(shù)字
count: 第一頁 10 第二/三頁 20
代碼展示
數(shù)據(jù)采集
'''
遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:926207505
尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書!
'''
# 導入數(shù)據(jù)請求模塊
import requests
import pandas as pd# 創(chuàng)建空列表
lis = []
def get_content(max_id):"""1. 發(fā)送請求 -> 模擬瀏覽器對于url地址發(fā)送請求"""# 模擬瀏覽器headers = {# Referer 防盜鏈'Referer':'https://weibo.com/2803301701/NxcPMvW2l',# User-Agent 用戶代理'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'}"""https://weibo.com/ajax/statuses/buildComments?is_reload=1&id=4979141627611265&is_show_bulletin=2&is_mix=0&count=10&uid=2803301701&fetch_level=0&locale=zh-CN- 問號前面: 請求鏈接- 問號后面: 查詢參數(shù)"""# 請求網(wǎng)址url = 'https://weibo.com/ajax/statuses/buildComments'# 請求參數(shù)data = {'is_reload': '1','id': '4979141627611265','is_show_bulletin': '2','is_mix': '0','max_id': max_id,'uid': '2803301701','fetch_level': '0','locale': 'zh-CN',}# 發(fā)送請求response = requests.get(url=url, params=data, headers=headers)"""2. 獲取數(shù)據(jù) -> 獲取服務器返回響應數(shù)據(jù)"""json_data = response.json()"""3. 解析數(shù)據(jù) -> 提取評論內(nèi)容"""# 提取評論所在列表content_list = json_data['data']# for循環(huán)遍歷, 提取列表里面元素for index in content_list:content = index['text_raw']dit = {'內(nèi)容': content}lis.append(dit)"""保存數(shù)據(jù)"""with open('data.txt', mode='a', encoding='utf-8') as f:f.write(content)f.write('\n')print(content)next_num = json_data['max_id']return next_numif __name__ == '__main__':lis = []max_id = ''for page in range(1, 11):max_id = get_content(max_id)df = pd.DataFrame(lis)df.to_excel('data.xlsx', index=False)
制作詞云圖
'''
遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:926207505
尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書!
'''
# 導入結(jié)巴分詞
import jieba
# 導入詞云圖模塊
import wordcloud"""詞云分析"""
# 讀取文件內(nèi)容
f = open('data.txt', encoding='utf-8').read()
# 分詞
txt = jieba.lcut(f)
# 把列表合并成字符串
string = ' '.join(txt)
# 制作詞云圖配置
wc = wordcloud.WordCloud(font_path='msyh.ttc',width=1000, # 寬height=700, # 高background_color='white', # 背景顏色 默認黑色
)
# 導入內(nèi)容
wc.generate(string)
wc.to_file('詞云_3.png')
print(txt)
尾語
最后感謝你觀看我的文章吶~本次航班到這里就結(jié)束啦 🛬
希望本篇文章有對你帶來幫助 🎉,有學習到一點知識~
躲起來的星星🍥也在努力發(fā)光,你也要努力加油(讓我們一起努力叭)。