python爬虫爬取豆瓣影评
时间: 2023-07-11 09:56:52 浏览: 191
好的,下面是一个简单的Python爬虫爬取豆瓣电影《肖申克的救赎》的影评的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://movie.douban.com/subject/1292052/comments?status=P'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
comments = soup.findAll('span', {'class': 'short'})
for comment in comments:
print(comment.getText())
```
这个代码使用了requests和BeautifulSoup库来爬取网页和解析网页内容。首先,我们定义了要爬取的网址和请求头。然后,我们使用requests库发送HTTP GET请求并获得响应。接着,我们使用BeautifulSoup库解析响应的HTML内容,并使用findAll方法找到所有的短评,并打印输出。
相关问题
python爬虫爬取豆瓣影评,要求有id,评分,内容
好的,你可以使用Python的requests和BeautifulSoup库来实现这个功能。以下是一个简单的代码示例:
```python
import requests
from bs4 import BeautifulSoup
# 设置请求头,模拟浏览器访问
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 豆瓣电影的URL
url = 'https://movie.douban.com/subject/26794435/comments?start=0&limit=20&sort=new_score&status=P'
# 发送GET请求
response = requests.get(url, headers=headers)
# 解析HTML
soup = BeautifulSoup(response.content, 'html.parser')
# 获取所有评论标签
comments = soup.find_all('div', class_='comment')
# 遍历每个评论标签
for comment in comments:
# 获取id
comment_id = comment.find('a')['href'].split('/')[-2]
# 获取评分
rating = comment.find('span', class_='rating').text.strip()
# 获取内容
content = comment.find('span', class_='short').text.strip()
# 输出结果
print('ID:', comment_id)
print('评分:', rating)
print('内容:', content)
```
这个代码可以爬取豆瓣电影《流浪地球》的影评,你可以根据需要修改URL和请求参数来爬取其他电影的影评。同时,你也可以将获取到的数据保存到文件或数据库中,以便后续的分析和应用。
python爬虫爬取豆瓣哪吒影评
### 编写Python爬虫抓取豆瓣电影《哪吒》的影评数据
#### 使用`requests`库发送HTTP请求并获取网页内容
为了访问目标页面,可以使用`requests`库向指定URL发起GET请求。由于豆瓣网站可能对频繁请求有所限制,建议设置合理的headers模拟浏览器行为。
```python
import requests
url = 'https://movie.douban.com/subject/26794435/comments' # 假设这是《哪吒》影片对应的短评链接
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
html_content = response.text
```
#### 解析HTML文档结构提取所需信息
通过分析网页源码得知,《哪吒》影评位于特定div容器内,可借助`lxml`解析器配合XPath表达式精准定位到每条评论节点及其子元素——用户名、评分星级以及正文部分。
```python
from lxml import etree
tree = etree.HTML(html_content)
comments_list = []
for item in tree.xpath('//div[@class="comment-item"]'):
user_name = ''.join(item.xpath('.//span[@class="comment-info"]/a/text()')).strip()
star_rating = len(item.xpath('.//span[contains(@class,"allstar")]/@class')) or None
comment_text = ''.join(item.xpath('.//p[@class=""]/text()')).replace('\n', '').strip()
comments_list.append({
'user': user_name,
'rating': star_rating,
'content': comment_text
})
```
#### 数据存储处理
最后一步就是将收集好的影评保存下来供后续研究分析之用。这里简单地采用JSON文件格式作为输出介质。
```python
import json
with open('nezzar_comments.json', mode='w+', encoding='utf-8') as f:
json.dump(comments_list, fp=f, ensure_ascii=False, indent=4)
print(f"成功导出了{len(comments_list)}条有效评论")
```
以上代码片段展示了如何构建一个基本版的Python爬虫用于采集特定主题下的用户反馈意见[^1]。需要注意的是,在实际开发过程中应当遵循各站点的服务条款,尊重版权方权益,合理合法地运用此类技术手段。
阅读全文
相关推荐
















