今天爬取的是騰訊視頻的最新短評(píng)(鬼吹燈之黃皮子墳)的評(píng)論者、最新短評(píng)內(nèi)容掉缺、評(píng)論贊态秧;
首先分析一下要爬取的網(wǎng)頁(yè)
首次加載出的最新短評(píng)
點(diǎn)擊更多,看下url是否會(huì)變化
點(diǎn)擊加載更多之后
由此看出應(yīng)該是動(dòng)態(tài)加載的
其實(shí)剛開(kāi)始我還看了這些數(shù)據(jù)是json店煞,還想著用構(gòu)造url,直接請(qǐng)求的方法去爬风钻,但是看完請(qǐng)求url就放棄了顷蟀;
Paste_Image.png
Paste_Image.png
這里url有太多的參數(shù)不好構(gòu)造就沒(méi)有往這方面想了;
下面進(jìn)入正題骡技,
首先pip安裝selenium
下面是代碼
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
import pymongo
# 連接到Mongo
conn = pymongo.MongoClient(host='localhost', port=27017)
# 選擇或創(chuàng)建數(shù)據(jù)庫(kù)
tencent = conn['tencent']
# 選擇或創(chuàng)建數(shù)據(jù)集合
newsdata = tencent['鬼吹燈之黃皮子墳']
# 把每個(gè)評(píng)論的數(shù)據(jù)存入這個(gè)字典
comment_one = {}
# 使用Chrome瀏覽器模擬打開(kāi)網(wǎng)頁(yè)鸣个,但是要把下載的chromedriver.exe放在python的文件路徑下,
#調(diào)試好之后換成PhantomJs,速度應(yīng)該會(huì)快一點(diǎn)
driver = webdriver.Chrome()
driver.get("https://v.qq.com/tv/p/topic/gcdzhpzf/index.html")
# 下拉滑動(dòng)瀏覽器屏幕羞反,具體下拉多少根據(jù)自己實(shí)際情況決定
driver.execute_script("window.scrollBy(0,10000)")
time.sleep(8)
driver.execute_script("window.scrollBy(0,6000)")
time.sleep(3)
i = 0
# 這里必須要先切到你要爬取數(shù)據(jù)的frame下,否則找不到你寫(xiě)的路徑
driver.switch_to.frame('commentIframe1')
# 這里我用的是find_elements_by_xpath匹配的元素囤萤,個(gè)人比較喜歡用xpath匹配丹诀,比較簡(jiǎn)單方便
comments_name = driver.find_elements_by_xpath(
'//div[@class="np-post-header"]/span[1]/a[1]')
comments_content = driver.find_elements_by_xpath(
'//div[@id="allComments"]//div[@class="np-post-content"]')
comments_vote = driver.find_elements_by_xpath(
'//div[@class="np-post-footer"]/a[1]/em')
print("第%s頁(yè)" % i)
#存入字典
for comment_name, comment_content, comment_vote in zip(
comments_name[-10: ], comments_content[-10: ], comments_vote[-10: ]):
comment_one = {
'評(píng)論者': comment_name.text,
'評(píng)論內(nèi)容': comment_content.text,
'評(píng)論贊': comment_vote.text
}
# 把數(shù)據(jù)插入數(shù)據(jù)庫(kù)
newsdata.insert_one(comment_one)
#下面是爬取多頁(yè)的評(píng)論數(shù)據(jù)松靡,同上
while driver.find_element_by_xpath('//div[@id="loadMore"]').text == '加載更多':
page_a = len(comments_name)
driver.find_element_by_xpath('//div[@id="loadMore"]').click()
time.sleep(5)
driver.execute_script("window.scrollBy(0,6000)")
time.sleep(3)
comments_name = driver.find_elements_by_xpath(
'//div[@class="np-post-header"]/span[1]/a[1]')
comments_content = driver.find_elements_by_xpath(
'//div[@id="allComments"]//div[@class="np-post-content"]')
comments_vote = driver.find_elements_by_xpath(
'//div[@class="np-post-footer"]/a[1]/em')
page_b = len(comments_name)
i += 1
print("第%s頁(yè)" % i)
for comment_name, comment_content, comment_vote in zip(
comments_name[page_a:page_b], comments_content[page_a:page_b], comments_vote[page_a:page_b]):
comment_one = {
'評(píng)論者': comment_name.text,
'評(píng)論內(nèi)容': comment_content.text,
'評(píng)論贊': comment_vote.text
}
newsdata.insert_one(comment_one)
運(yùn)行結(jié)果:大概爬了4000多條評(píng)論谣殊,應(yīng)該沒(méi)有丟失的
Paste_Image.png
這里有幾點(diǎn)注意一下:
1箩言、爬取多頁(yè)的最新短評(píng),元素的匹配是否正常富雅,雖然方法很多掸驱,因?yàn)檫@里相同名字元素太多了;
2没佑、選擇列表的數(shù)據(jù)毕贼,這里首頁(yè)是10個(gè),點(diǎn)擊一次加載更多是20條評(píng)論图筹,但是你打開(kāi)json數(shù)據(jù)key看到有些數(shù)據(jù)不知道為什么是空的帅刀,在網(wǎng)頁(yè)上沒(méi)有展示出來(lái)的,可能是有些評(píng)論異常吧远剩,所以這里如果是固定的每次累加20個(gè)取數(shù)據(jù)的話扣溺,取出的數(shù)據(jù)應(yīng)該是有問(wèn)題的,
最后瓜晤,大家有什么好的方法可以評(píng)論建議一下锥余,多謝