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

爬取豆瓣电影的数据

首先打开"豆瓣电影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()

结果

相关推荐
奔跑吧邓邓子8 小时前
【Python爬虫(34)】Python多进程编程:开启高效并行世界的钥匙
开发语言·爬虫·python·多进程
dme.11 小时前
Python爬虫selenium验证-中文识别点选+图片验证码案例
爬虫·python
B站计算机毕业设计超人12 小时前
计算机毕业设计Hadoop+Spark+DeepSeek-R1大模型民宿推荐系统 hive民宿可视化 民宿爬虫 大数据毕业设计(源码+LW文档+PPT+讲解)
大数据·hadoop·爬虫·机器学习·课程设计·数据可视化·推荐算法
风123456789~12 小时前
【爬虫基础】第一部分 网络通讯-编程 P3/3
网络·爬虫
奔跑吧邓邓子13 小时前
【Python爬虫(44)】分布式爬虫:筑牢安全防线,守护数据之旅
开发语言·分布式·爬虫·python·安全
奔跑吧邓邓子14 小时前
【Python爬虫(45)】Python爬虫新境界:分布式与大数据框架的融合之旅
开发语言·分布式·爬虫·python·大数据框架
奔跑吧邓邓子1 天前
【Python爬虫(36)】深挖多进程爬虫性能优化:从通信到负载均衡
开发语言·爬虫·python·性能优化·负载均衡·多进程
奔跑吧邓邓子1 天前
【Python爬虫(27)】探索数据可视化的魔法世界
开发语言·爬虫·python·数据可视化
Java开发-楠木1 天前
爬虫破解网页禁止F12
爬虫
数据小爬虫@1 天前
爬虫获取的数据能用于哪些数据分析?
爬虫·数据挖掘·数据分析