[使用案例]通過使用ip代理騰訊視頻評論爬蟲案例
對于專業(yè)的爬蟲工程師來說,掌握好爬蟲語言以及ip代理的配合使用,能夠爬取到不同網(wǎng)站的不同信息。今天我們來看看一個具體的爬蟲案例。
如何爬取騰訊視頻的評論?下面跟著IP海來看看具體的操作步驟:
在火狐瀏覽器打開騰訊視頻,比如https://v.qq.com/x/cover/j6cgzhtkuonf6te.html
點擊查看更多解讀,這時fiddler會有一個js文件:
里面的內(nèi)容就是評論。
找到一條評論轉(zhuǎn)一下碼:
在火狐里ctrl+f看看有沒有這條評論。
copy js文件的url。
點擊查看更多評論,再觸發(fā)一個json,copy url
分析兩個url:
簡化一下網(wǎng)頁試試:https://video.coral.qq.com/filmreviewr/c/upcomment/j6cgzhtkuonf6te?reqnum=3&commentid=6227734628246412645
通過分析,我們可以知道j6cg……是視頻id,reqnum是每次查看的評論數(shù)量,commentid是評論id
https://video.coral.qq.com/filmreviewr/c/upcomment/【vid】?reqnum=【num】&commentid=【cid】
單頁評論爬蟲
有一些特殊字符比如圖片現(xiàn)在還不知道怎么處理……以后再說吧
import urllib.request
import re
from uaip import *
vid="j6cgzhtkuonf6te"
cid="6227734628246412645"
num="3" #每頁提取3個
url="https://video.coral.qq.com/filmreviewr/c/upcomment/"+vid+"?reqnum="+num+"&commentid="+cid
data=ua_ip(url)
titlepat='"title":"(.*?)","abstract":"'
commentpat='"content":"(.*?)",'
titleall=re.compile(titlepat,re.S).findall(data)
commentall=re.compile(commentpat,re.S).findall(data)
# print(len(commentall))
for i in range(len(titleall)):
try:
print("評論標(biāo)題是:"+eval("u'"+titleall[i]+"'"))
print("評論內(nèi)容是:"+eval("u'"+commentall[i]+"'"))
print('---------------')
except Exception as err:
print(err)
翻頁評論爬蟲
查看網(wǎng)頁源代碼可以發(fā)現(xiàn)last:后面的內(nèi)容為下一頁的id
import urllib.request
import re
from uaip import *
vid="j6cgzhtkuonf6te"
cid="6227734628246412645"
num="3"
for j in range(10): #爬取1~10頁內(nèi)容
print("第"+str(j+1)+"頁")
url = "https://video.coral.qq.com/filmreviewr/c/upcomment/" + vid + "?reqnum=" + num + "&commentid=" + cid
data = ua_ip(url)
titlepat = '"title":"(.*?)","abstract":"'
commentpat = '"content":"(.*?)",'
titleall = re.compile(titlepat, re.S).findall(data)
commentall = re.compile(commentpat, re.S).findall(data)
lastpat='"last":"(.*?)"'
cid=re.compile(lastpat,re.S).findall(data)[0]
for i in range(len(titleall)):
try:
print("評論標(biāo)題是:" + eval("u'" + titleall[i] + "'"))
print("評論內(nèi)容是:" + eval("u'" + commentall[i] + "'"))
print('---------------')
except Exception as err:
print(err)
對于短評(普通評論)方法類似,這里就不贅述了,看下面這個短評爬蟲代碼:
將https://video.coral.qq.com/varticle/1743283224/comment/v2?callback=_varticle1743283224commentv2&orinum=10&oriorder=o&pageflag=1&cursor=6442954225602101929&scorecursor=0&orirepnum=2&reporder=o&reppageflag=1&source=132&_=1566363507957
簡化成:https://video.coral.qq.com/varticle/1743283224/comment/v2?orinum=10&oriorder=o&pageflag=1&cursor=6442954225602101929
import urllib.request
import re
from uaip import *
vid="1743283224"
cid="6442954225602101929"
num="5"
for j in range(10): #爬取1~10頁內(nèi)容
print("第"+str(j+1)+"頁")
url="https://video.coral.qq.com/varticle/"+vid+"/comment/v2?orinum="+num+"&oriorder=o&pageflag=1&cursor="+cid
data = ua_ip(url)
commentpat = '"content":"(.*?)"'
commentall = re.compile(commentpat, re.S).findall(data)
lastpat='"last":"(.*?)"'
cid=re.compile(lastpat,re.S).findall(data)[0]
# print(len(gg))
# print(len(commentall))
for i in range(len(commentall)):
try:
print("評論內(nèi)容是:" + eval("u'" + commentall[i] + "'"))
print('---------------')
except Exception as err:
print(err)
以上,我們就將騰訊視頻的評論內(nèi)容給抓取下來了,大家可以自己練習(xí)看看效果。
版權(quán)聲明:本文為IP海(iphai.cn)原創(chuàng)作品,未經(jīng)許可,禁止轉(zhuǎn)載!
Copyright © www.wibm.ac.cn. All Rights Reserved. IP海 版權(quán)所有.
IP海僅提供中國內(nèi)IP加速服務(wù),無法跨境聯(lián)網(wǎng),用戶應(yīng)遵守《服務(wù)條款》內(nèi)容,嚴(yán)禁用戶使用IP海從事任何違法犯罪行為。
鄂ICP備19030659號-3
鄂公網(wǎng)安備42100302000141號
計算機(jī)軟件著作權(quán)證
ICP/EDI許可證:鄂B2-20200106