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