爬取豆瓣电影的数据-----爬虫实战案例(爬取文字)

爬取豆瓣电影的数据

首先打开"豆瓣电影TOP250"网页:

  • 右击鼠标,找到检查点击,然后再点击网络
  • 向上拉动,找到名称栏中的第一个,点击打开
  • 可以在标头里看到请求URL和请求方式,复制URL(需要用到)
  • 在标头的最下面有"User-Agent",也复制下来(也可以下载pip install fake-userangent库,用别人写好的UA)


确定我们要爬取的内容

我们爬取每个电影的一些信息

获取多页的信息

因为豆瓣电影这个页面每一个页面只能展示25个电影,当点击下一页时我们会发现每一页的网址只有25->50->75这样的变化,其他的没有变化,所以变化可以通过for循环来遍历它,从而达到**获取到多页的电影信息 **


具体代码如下:

python 复制代码
for i in range(0, 250, 25):
        url = f"https://movie.douban.com/top250?start={i}&filter="

完整代码:

python 复制代码
# 豆瓣电影
import requests   # 别人写好的ua  pip install fake_useragent
import fake_useragent
from lxml import etree
import re

if __name__ == '__main__':
    # UA伪装
    head = {
        # "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0"
        # 用 import fake_useragent 库里的UA
        "User-Agent": fake_useragent.UserAgent().random
    }
    
    # 打开一个文件将获取的数据写进去
    fp = open("./doubanFilm.txt", "w", encoding="utf-8")

    # 1、url
    # url = "https://movie.douban.com/top250"
    # url2 = "https://movie.douban.com/top250?start=25&filter="
    # url3 = "https://movie.douban.com/top250?start=50&filter="
    for i in range(0, 250, 25):
        url = f"https://movie.douban.com/top250?start={i}&filter="

        # 发送请求
        response = requests.get(url, headers=head)
        # 获取想要的数据
        res_text = response.text
        # print(res_text)
        # 数据解析
        tree = etree.HTML(res_text)
        # 定位所有的li标签
        li_list = tree.xpath("//ol[@class='grid_view']/li")
        for li in li_list:
            # 获取电影名字
            film_name = "".join(li.xpath(".//span[@class='title'][1]/text()"))
            # 获取导演、主演、年份、国家、电影类型
            director_actor_y_country_type = "".join(li.xpath(".//div[@class='bd']/p[1]/text()"))
            # 获取评分
            score = "".join(li.xpath(".//span[@class='rating_num']/text()"))
            # 获取标签语
            quote = "".join(li.xpath(".//span[@class='inq']/text()"))
            # 将获取到的导演、主演、年份、国家、电影类型放到一个新列表里,并去除空格
            new_str = director_actor_y_country_type.strip()
            # 将导演从新列表中取出  用正则表达式去匹配
            director = re.match(r"导演: ([a-zA-Z\u4e00-\u9fa51]+)(.*?)", new_str).group(1)
            # 取年份
            y = re.match(r"([\s\S]+?)(\d+)(.*?)", new_str).group(2)
            # 取国家
            country = new_str.rsplit("/")[-2].strip()
            # 取电影类型
            types = new_str.rsplit("/")[-1].strip()
            # 因为有些电影没有主演所以将主演用try-except方法做判断,有主演的取出来,没有主演设定为"no"
            try:
                actor = re.match(r"(.*?)主演: ([a-zA-Z\u4e00-\u9fa5·]+)(.*?)", new_str).group(2)
            except Exception as e:
                actor = "no"
            
            # 因为每个数据都用.join的方法转换成了字符串,所以便可以将这些数据相加并用#隔开,便能连在一起,并换行,使得数据更加工整
            fp.write(film_name + "#" + director + "#" + actor + "#" + y + "#"
                + country + "#" + types + "#" + score + "#" + quote + "\n")
            print(film_name, director, actor, y, country, types, score, quote)
    fp.close()

结果

相关推荐
喵手19 小时前
Python爬虫实战:HTTP缓存系统深度实战 — ETag、Last-Modified与requests-cache完全指南(附SQLite持久化存储)!
爬虫·python·爬虫实战·http缓存·etag·零基础python爬虫教学·requests-cache
喵手19 小时前
Python爬虫实战:容器化与定时调度实战 - Docker + Cron + 日志轮转 + 失败重试完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·容器化·零基础python爬虫教学·csv导出·定时调度
喵手21 小时前
Python爬虫实战:全站 Sitemap 自动发现 - 解析 sitemap.xml → 自动生成抓取队列的工业级实现!
爬虫·python·爬虫实战·零基础python爬虫教学·sitemap·解析sitemap.xml·自动生成抓取队列实现
iFeng的小屋1 天前
【2026年新版】Python根据小红书关键词爬取所有笔记数据
笔记·爬虫·python
Love Song残响1 天前
揭秘Libvio爬虫:动态接口与逆向实战
爬虫
喵手1 天前
Python爬虫实战:构建招聘会数据采集系统 - requests+lxml 实战企业名单爬取与智能分析!
爬虫·python·爬虫实战·requests·lxml·零基础python爬虫教学·招聘会数据采集
iFeng的小屋1 天前
【2026最新当当网爬虫分享】用Python爬取千本日本相关图书,自动分析价格分布!
开发语言·爬虫·python
数研小生1 天前
关键词搜索京东列表API技术对接指南
大数据·数据库·爬虫
喵手1 天前
Python爬虫实战:网页截图归档完全指南 - 构建生产级页面存证与历史回溯系统!
爬虫·python·爬虫实战·零基础python爬虫教学·网页截图归档·历史回溯·生产级方案
Blurpath住宅代理1 天前
动态代理的五大优点:提升爬虫效率与安全性
网络·爬虫·动态ip·住宅ip·住宅代理