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