【实战项目二】Python爬取豆瓣影评

目录

一、环境准备

二、编写代码


一、环境准备

bash 复制代码
pip install beautifulsoup4
pip intall lxml
pip install requests

我们需要爬取这些影评

二、编写代码

我们发现每个影评所在的div的class都相同,我们可以从这入手

python 复制代码
from bs4 import BeautifulSoup
import requests

# 请求头信息
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
}
url = 'https://movie.douban.com/review/best/'
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'lxml')
div = soup.find_all('div', class_='main review-item') # 找到每个影评的div,返回所有影评div列表
for d in div:
    # 获取所有img标签
    img = d.img
    # 获取硬盘标题
    title = d.h2.string
    # 获取影评
    con = list(d.find('div', class_="short-content").stripped_strings)[0]
    print(con)
相关推荐
郑州光合科技余经理14 小时前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo12314 小时前
matlab画图工具
开发语言·matlab
dustcell.14 小时前
haproxy七层代理
java·开发语言·前端
norlan_jame14 小时前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone15 小时前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ40220549615 小时前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
百锦再15 小时前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
QQ51100828515 小时前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php
QQ_196328847515 小时前
Python-flask框架西山区家政服务评价系统网站设计与开发-Pycharm django
python·pycharm·flask
遥遥江上月15 小时前
Node.js + Stagehand + Python 部署
开发语言·python·node.js