我想輸入某個B站番號,然后爬取其中視頻的彈幕,我采用的方法來源于知乎
b站的視頻有兩個相關(guān)的id number,一個是cid,一個是我們都知道的AV number。抓取彈幕需要的是cid。最簡單的方法是直接查看網(wǎng)頁源代碼搜索cid,這樣就可以得到單獨(dú)一個視頻的cid了。然后通過這個網(wǎng)址就可以得到相關(guān)視頻的彈幕“http://comment.bilibili.com/“ + cid + “.xml“。 不過問題在于,因?yàn)閎站彈幕的設(shè)置問題,每個視頻是有一個彈幕上限的。 如果數(shù)量到達(dá)一定數(shù)量,就會被清理。所以這個方法只能得到香瓜能視頻下面現(xiàn)在的彈幕。 當(dāng)然如果批量處理的話,需要用正則去搜索這個cid,然后通過這個url去處理一下xml structure的文件。
作者:劉岳
鏈接:https://www.zhihu.com/questio...
來源:知乎
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
我的代碼如下:
#1表示選擇使用正則
#2表示選擇使用xpath
import requests
import re
from lxml import etree
#抓取網(wǎng)頁
def getHTML(url,op):
header={"User-Agent":'Mozilla/5.0'}
try:
r = requests.get(url,timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
if(op==1):return r.text
else:return r.content
except:
return "抓取失?。?
#獲取cid信息
def paraHTML(html):
cid = re.findall(r'cid\=(.*)\&aid',html)
return cid
def print_comment(html):
comments = etree.HTML(html)
print(len(comments))
def main():
raw_url = "https://www.bilibili.com/video/"
av_number = input("請輸入B站AV番號")
url = raw_url+av_number
html = getHTML(url,1)
cid = paraHTML(html)[0]
print(type(cid))
comment_url = "http://comment.bilibili.com/cid.xml"
comment_url = comment_url.replace("cid",cid)
print(comment_url)
comment_html = getHTML(comment_url,2)
print_comment(comment_html)
main()
彈幕的那個網(wǎng)站,右擊查看屬性,彈幕內(nèi)容全在一個個p標(biāo)簽里,我一開始用xpath的方法抓取,卻抓取不了,為空,后來我直接打印出其中的長度,發(fā)現(xiàn)為1,并且是一個空列表,這是為什么?
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。