# 需要使用requests 和 Beautiful Soup,而且只需一个函数就行 |
# 如果需要爬取多个页面,可以使用`while`循环来迭代访问多个页面,然后将每一页抓取到的数据以`append`的方式来添加到列表中即可 |
import requests |
from bs4 import BeautifulSoup |
def crawl_comments(url): |
comments = [] |
page = requests.get(url) |
soup = BeautifulSoup(page.text, 'html.parser' ) |
for item in soup.find_all( 'div' , class_ = 'comment-item' ): |
comment = item.find( 'p' , class_ = 'comment-body' ).text |
comments.append(comment) |
return comments |