python爬取机器学习评论
时间: 2023-06-14 19:04:10 浏览: 47
要爬取机器学习评论,可以使用Python中的爬虫框架如Scrapy或BeautifulSoup来实现。
以下是一个使用BeautifulSoup的示例代码,假设我们要爬取CSDN上机器学习文章的评论:
```python
import requests
from bs4 import BeautifulSoup
url = "https://blog.csdn.net/nav/machinelearning"
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html, 'html.parser')
articles = soup.find_all("h4", class_="text-truncate")
for article in articles:
article_url = article.a['href']
article_response = requests.get(article_url)
article_html = article_response.content
article_soup = BeautifulSoup(article_html, 'html.parser')
comments = article_soup.find_all("div", class_="comment-box")
for comment in comments:
print(comment.p.text)
```
代码中,我们首先访问CSDN机器学习文章列表页面,然后使用BeautifulSoup解析页面。接着,我们找到所有文章标题,并依次访问每篇文章的页面。在每篇文章的页面中,我们找到所有评论框的标签,并输出评论内容。