前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理
以下文章來源于菜鳥學Python資料分析
1.網頁分析
本文以爬取《脫口秀大會 第3季》最后一期視頻彈幕為例,首先通過以下步驟找到存放彈幕的真實url,
通過刪減各引數,發現僅有timestamp引數的變化會影響彈幕資料的爬取,且timestamp引數是首項為15,公差為30的等引數列,可以大膽猜測騰訊視頻每30秒更新一頁彈幕資料,該視頻長度為12399秒,而資料格式為標準的json格式,因此json.loads直接決議資料即可,
2.爬蟲實戰
import requests import json import time import pandas as pd df = pd.DataFrame() for page in range(15, 12399, 30): headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'} url = 'https://mfm.video.qq.com/danmu?otype=json×tamp={}&target_id=5938032297%26vid%3Dx0034hxucmw&count=80'.format(page) print("正在提取第" + str(page) + "頁") html = requests.get(url,headers = headers) bs = json.loads(html.text,strict = False) #strict引數解區域分內容json格式決議報錯 time.sleep(1) #遍歷獲取目標欄位 for i in bs['comments']: content = i['content'] #彈幕 upcount = i['upcount'] #點贊數 user_degree =i['uservip_degree'] #會員等級 timepoint = i['timepoint'] #發布時間 comment_id = i['commentid'] #彈幕id cache = pd.DataFrame({'彈幕':[content],'會員等級':[user_degree], '發布時間':[timepoint],'彈幕點贊':[upcount],'彈幕id':[comment_id]}) df = pd.concat([df,cache]) df.to_csv('tengxun_danmu.csv',encoding = 'utf-8') print(df.shape)
3.資料預覽
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/180409.html
標籤:其他
