要實(shí)現(xiàn)爬取連續(xù)多頁(yè)的評(píng)論,可以使用Python的requests庫(kù)和BeautifulSoup庫(kù)。以下是一個(gè)簡(jiǎn)單的示例:
- 安裝所需的庫(kù):
pip install requests beautifulsoup4
- 然后,編寫(xiě)代碼實(shí)現(xiàn)爬取連續(xù)多頁(yè)的評(píng)論:
import requests
from bs4 import BeautifulSoup
# 請(qǐng)求第一頁(yè)評(píng)論
url = 'https://example.com/comments' # 替換為實(shí)際評(píng)論頁(yè)面的URL
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 提取評(píng)論
comments = soup.find_all('div', class_='comment')
# 遍歷并打印評(píng)論內(nèi)容
for comment in comments:
print(comment.text)
# 請(qǐng)求第二頁(yè)評(píng)論
url = 'https://example.com/comments?page=2' # 替換為實(shí)際評(píng)論頁(yè)面的URL
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 提取評(píng)論
comments = soup.find_all('div', class_='comment')
# 遍歷并打印評(píng)論內(nèi)容
for comment in comments:
print(comment.text)
注意:請(qǐng)根據(jù)實(shí)際情況修改代碼中的URL、CSS選擇器等。
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。