獲取b站視頻評(píng)論和子評(píng)論儲(chǔ)存csv-以周菲戈我不相信為例
#? 打開網(wǎng)頁 抓取b站網(wǎng)頁內(nèi)容 保存為評(píng)論html 方法此處不再贅述

# 分析網(wǎng)頁代碼 找出儲(chǔ)存評(píng)論的節(jié)點(diǎn)
# 評(píng)論 class='root-reply-container'??.reply-content? span
# 子評(píng)論 class='sub-reply-container'??.reply-content span
from pyquery import PyQuery
import csv
with open('評(píng)論.html', 'r', encoding='utf-8') as fr:
? ?html = fr.read()
doc = PyQuery(html)
div = doc('.root-reply-container')
span_list = div('.reply-content')
ls = []
for span in span_list.items():
? ?ls.append(span.text())
div_2 = doc('.sub-reply-container')
ls2 = []
span_list_2 = div_2('.reply-content')
for span_2 in span_list_2.items():
? ?ls2.append(span_2.text())

# 把評(píng)論儲(chǔ)存為csv文件 雖然不知道為啥強(qiáng)迫自己要這樣子存
with open('評(píng)論.csv', 'w', encoding='utf-8', newline='') as f:
? ?writer = csv.writer(f)
? ?header = "評(píng)論", "子評(píng)論"
? ?writer.writerow(header)
? ?for ls_1, ls_2?in zip(ls, ls2):
? ? ? ?data = ''.join(ls_2)
? ? ? ?writer.writerow([is_1]+[data])

# 今天是沒有私貨的一天,難受!

