基于Python的爬虫演示示例-以电影网站为例

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 爬取的数据最终会存到MYSQL服务器的表中,可以写程序将数据展示出来。注意的时会对IP进行限制封号,200条为限,超过IP会被限制,可以换一个IP进行抓去。主要研究爬虫的基本使用规范和语法,相对较为简单,供大家学习参考

一,项目简介

  基于Python实现豆瓣电影数据的抓去,并存入本在数据库。

  数据库结构准备:

 

create table if not exists `categories` (
    `id` int(11) NOT NULL PRIMARY KEY,
    `type` varchar (255) NOT NULL DEFAULT ''
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
create table if not exists `movies`(
    `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `cover` varchar (255) NOT NULL DEFAULT '',
    `title` varchar (50) NOT NULL DEFAULT '',
    `date` varchar (10) NOT NULL DEFAULT '',
    `rate` float DEFAULT 0,
    `director` varchar (100) NOT NULL DEFAULT '',
    `scriptwriter` varchar(100) NOT NULL DEFAULT '',
    `actors` text,
    `district` varchar(255) DEFAULT '',
    `language` varchar (255) DEFAULT '',
    `duration` varchar (100) DEFAULT '',
    `abs` text,
    UNIQUE (`title`)
)ENGINE=InnoDB  DEFAULT CHARSET=utf8;
create table if not exists `movie-category` (
    `id` BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `mid` int(11) NOT NULL,
    `cid` int(11) NOT NULL,
    KEY `fk_on_movie_id` (`mid`),
    CONSTRAINT `fk_on_movie_id` FOREIGN KEY (`mid`) REFERENCES `movies` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    KEY `fk_on_category_id` (`cid`),
    CONSTRAINT `fk_on_category_id` FOREIGN KEY (`cid`) REFERENCES `categories` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB  DEFAULT CHARSET=utf8;

image.gif

INSERT INTO `categories` VALUES (1,'剧情');
INSERT INTO `categories` VALUES (2,'喜剧');
INSERT INTO `categories` VALUES (3,'动作');
INSERT INTO `categories` VALUES (4,'爱情');
INSERT INTO `categories` VALUES (5,'科幻');
INSERT INTO `categories` VALUES (6,'动画');
INSERT INTO `categories` VALUES (7,'悬疑');
INSERT INTO `categories` VALUES (8,'惊悚');
INSERT INTO `categories` VALUES (9,'恐怖');
INSERT INTO `categories` VALUES (10,'犯罪');
INSERT INTO `categories` VALUES (11,'同性');
INSERT INTO `categories` VALUES (12,'音乐');
INSERT INTO `categories` VALUES (13,'歌舞');
INSERT INTO `categories` VALUES (14,'传记');
INSERT INTO `categories` VALUES (15,'历史');
INSERT INTO `categories` VALUES (16,'战争');
INSERT INTO `categories` VALUES (17,'西部');
INSERT INTO `categories` VALUES (18,'奇幻');
INSERT INTO `categories` VALUES (19,'冒险');
INSERT INTO `categories` VALUES (20,'灾难');
INSERT INTO `categories` VALUES (21,'武侠');
INSERT INTO `categories` VALUES (22,'情色');

image.gif

二,环境介绍

语言环境:Python3.7+scrapy

数据库:Mysql: mysql5.7

开发工具:IDEA或eclipse

三,核心代码展示

数据模型:items.py

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
import scrapy
class DoubanItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    #电影标题
    title = scrapy.Field()
    #导演
    director = scrapy.Field()
    #编剧
    scriptwriter = scrapy.Field()
    #演员
    actors = scrapy.Field()
    #上映日期
    date = scrapy.Field()
    #评分
    rate = scrapy.Field()
    #国家/地区
    district = scrapy.Field()
    #语言
    language = scrapy.Field()
    #封面图片
    cover = scrapy.Field()
    #简介
    abs = scrapy.Field()
    #类型
    categories = scrapy.Field()
    #时长
    duration = scrapy.Field()

image.gif

数据存储工具定义:pipelines.py

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface
from scrapy.exceptions import DropItem
from scrapy.http import Request
from scrapy.pipelines.images import ImagesPipeline
import pymysql
import random
class DoubanPipeline:
    def process_item(self, item, spider):
        return item
#根据取得的图片url重新请求,下载图片到本地
class DownloadImagePipeline(ImagesPipeline):
    default_headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36",
        #"Cookie":'_vwo_uuid_v2=D65EBF690D9454DE4C13354E37DC5B9AA|3bb7e6e65f20e31141b871b4fea88dc2; __yadk_uid=QBp8bLKHjCn5zS2J5r8xV7327R0wnqkU; douban-fav-remind=1; gr_user_id=0a41d8d1-fe39-4619-827a-17961cf31795; viewed="35013197_10769749_23008813_26282806_34912177_22139960_35003794_30249691_26616244_27035127"; push_noty_num=0; push_doumail_num=0; __utmv=30149280.21320; bid=gplG4aEN4Xc; ll="108288"; ap_v=0,6.0; __utma=30149280.819011260.1572087992.1604448803.1604453561.105; __utmc=30149280; __utmz=30149280.1604453561.105.65.utmcsr=accounts.douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __gads=ID=eddb65558a1da756-223ab4f88bc400c8:T=1604453562:RT=1604453562:S=ALNI_MZGB_I69qmiL2tt3lm57JVX1i4r2w; __utmb=30149280.4.10.1604453561; dbcl2="213202515:Ip9mjwUAab4"; ck=wxUS; __utma=223695111.897479705.1572088003.1604448803.1604455298.71; __utmb=223695111.0.10.1604455298; __utmc=223695111; __utmz=223695111.1604455298.71.42.utmcsr=accounts.douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1604455298%2C%22https%3A%2F%2Faccounts.douban.com%2F%22%5D; _pk_ses.100001.4cf6=*; _pk_id.100001.4cf6=e11874c5506d4ab1.1572088003.71.1604455342.1604450364.'
    }
    def get_media_requests(self, item, info):
        #print('到这里来了...')
        image_url = item['cover']
        yield Request(
            image_url,
            headers=self.default_headers)
    #get_media_requests函数返回后执行
    def item_completed(self, results, item, info):
        image_paths = [x['path'] for ok, x in results if ok]
        if not image_paths:
            raise DropItem("Item contains no images")
        #返回的图片地址是full+文件名的格式,由于我是边爬边下载,所以每次只有一张图片,但是返回的是
        #数组,函数设计为多张图片,我将‘full’替换成了自己后台接口的地址,方便数据库中的存储
        image_paths = str(image_paths[0]).replace('full','http://localhost:8443/api/file')
        item['cover'] = image_paths
        return item
# 将电影信息存入到数据库中
class DBPipeline(object):
    def __init__(self):
        # connection database
        # 后面三个依次是数据库连接名、数据库密码、数据库名称
        self.connect = pymysql.connect(host='127.0.0.1', user='root', password='root',
                                       db='fivesix',charset='utf8',port=3306)
        # get cursor
        self.cursor_1 = self.connect.cursor()
        self.cursor_2 = self.connect.cursor()
        self.type_to_id = {
            '剧情': 1,'喜剧':2, '动作':3,
            '爱情': 4, '科幻':5, '动画':6,
            '悬疑': 7, '惊悚' : 8, '恐怖' : 9,
            '犯罪': 10, '同性':11, '音乐':12,
            '歌舞':13, '传记':14,'历史':15,
            '战争':16, '西部':17, '奇幻':18,
            '冒险':19, '灾难':20,'武侠':21, '情色':22
        }
        print("连接数据库成功")
    def process_item(self, item, spider):
        if item['title'] == '':
            return
        # sql语句
        insert_movie_sql = """
        insert ignore into `movies`(cover,title, director, scriptwriter, actors, district,rate,date,language,duration,abs) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
        """
        insert_mc_sql = """
        insert into `movie-category` (mid,cid) values (%s,%s)
        """
        # 执行插入数据到数据库操作
        self.cursor_1.execute(insert_movie_sql, (item['cover'], item['title'], item['director'], item['scriptwriter'],
                                         item['actors'],item['district'],item['rate'],
                                         item['date'],item['language'],item['duration'],item['abs']))
        mid = self.cursor_1.lastrowid
        #处理标签
        cids = []
        categories = item['categories'].split('/')
        for c in categories:
            if c not in self.type_to_id.keys():continue
            cids.append(self.type_to_id.get(c))
        #插入关联表
        print(cids)
        for cid in cids:
            self.cursor_2.execute(insert_mc_sql,(mid,cid))
        # 提交,不进行提交无法保存到数据库
        self.connect.commit()
    def close_spider(self, spider):
        # 关闭游标和连接
        self.cursor_1.close()
        self.cursor_2.close()
        self.connect.close()

image.gif

爬虫核心代码:movies.py

# -*- coding: utf-8 -*-
import scrapy
import json
import re
import time
from douban.items import DoubanItem
from fake_useragent import UserAgent
import random
class MovieHotSpider(scrapy.Spider):
    #爬虫的名称,在命令行可以方便的运行爬虫
    name = "movie_hot"
    allowed_domains = ["movie.douban.com"]
    #pro = ['139.224.37.83','115.223.7.110','221.122.91.75']
    # 拼接豆瓣电影URL
    BASE_URL = 'https://movie.douban.com/j/search_subjects?type=movie&tag=%s&sort=recommend&page_limit=%s&page_start=%s'
    MOVIE_TAG = '华语'
    PAGE_LIMIT = 20
    page_start = 0
    domains = BASE_URL % (MOVIE_TAG, PAGE_LIMIT, page_start)
    #伪装浏览器
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
        #,"Cookie":'_vwo_uuid_v2=D65EBF690D9454DE4C13354E37DC5B9AA|3bb7e6e65f20e31141b871b4fea88dc2; __yadk_uid=QBp8bLKHjCn5zS2J5r8xV7327R0wnqkU; douban-fav-remind=1; gr_user_id=0a41d8d1-fe39-4619-827a-17961cf31795; viewed="35013197_10769749_23008813_26282806_34912177_22139960_35003794_30249691_26616244_27035127"; push_noty_num=0; push_doumail_num=0; __utmv=30149280.21320; bid=gplG4aEN4Xc; ll="108288"; ap_v=0,6.0; __utma=30149280.819011260.1572087992.1604448803.1604453561.105; __utmc=30149280; __utmz=30149280.1604453561.105.65.utmcsr=accounts.douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __gads=ID=eddb65558a1da756-223ab4f88bc400c8:T=1604453562:RT=1604453562:S=ALNI_MZGB_I69qmiL2tt3lm57JVX1i4r2w; __utmb=30149280.4.10.1604453561; dbcl2="213202515:Ip9mjwUAab4"; ck=wxUS; __utma=223695111.897479705.1572088003.1604448803.1604455298.71; __utmb=223695111.0.10.1604455298; __utmc=223695111; __utmz=223695111.1604455298.71.42.utmcsr=accounts.douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1604455298%2C%22https%3A%2F%2Faccounts.douban.com%2F%22%5D; _pk_ses.100001.4cf6=*; _pk_id.100001.4cf6=e11874c5506d4ab1.1572088003.71.1604455342.1604450364.'
    }
    #总共爬取的页数
    pages = 100
    # 爬虫从此开始
    def start_requests(self):
        print('~~~~爬取列表: '+ self.domains)
        yield scrapy.Request(
            url = self.domains,
            headers=self.headers,
            callback=self.request_movies
        )
    # 分析列表页
    def request_movies(self, response):
        infos = response.text
        # 使用JSON模块解析响应结果
        infos = json.loads(infos)
        # 迭代影片信息列表
        for movie_info in infos['subjects']:
            print('~~~爬取电影: ' + movie_info['title'] + '/'+ movie_info['rate'])
            # 提取影片页面url,构造Request发送请求,并将item通过meta参数传递给影片页面解析函数
            yield scrapy.Request(
                url = str(movie_info['url']),
                headers = self.headers,
                callback = self.request_movie,
                dont_filter=True
            )
        #如果已经爬完pages或者当前标签下没有更多电影时退出
        if self.pages > 0 and len(infos['subjects']) == self.PAGE_LIMIT:
            self.pages -= 1
            self.page_start += self.PAGE_LIMIT
            url = self.BASE_URL % (self.MOVIE_TAG,self.PAGE_LIMIT,self.page_start)
            time.sleep(5)
            print('-----爬取列表: ' + url)
            yield scrapy.Request(
                url=url,
                headers=self.headers,
                callback=self.request_movies,
                dont_filter=True
            )
    # 分析详情页
    def request_movie(self, response):
        #组装数据
        movie_item = DoubanItem()
        title = response.css('div#content>h1>span:nth-child(1)::text').extract_first()
        t = re.findall('[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b\u4e00-\u9fa5_0-9]', title)
        #获取非info区域数据
        movie_item['title'] = ''.join(t)
        movie_item['date'] = response.css('div#content>h1>span.year::text').extract_first()[1:-1]
        movie_item['rate'] = response.css('strong.rating_num::text').extract_first()
        #movie_item['commentCount'] = response.css('div.rating_sum>a.rating_people>span::text').extract_first()
        #movie_item['start'] = '/'.join(response.css('span.rating_per::text').extract())
        #movie_item['better'] = '/'.join(response.css('div.rating_betterthan>a::text').extract())
        movie_item['abs'] = response.css('#link-report>span::text').extract_first().strip()
        movie_item['cover'] = response.css('#mainpic>a>img::attr(src)').extract_first()
        # 获取整个信息字符串
        info = response.css('div.subject div#info').xpath('string(.)').extract_first()
        # 提取所以字段名
        fields = [s.strip().replace(':', '') for s in response.css('div#info span.pl::text').extract()]
        # 提取所有字段的值
        values = [re.sub('\s+', '', s.strip()) for s in re.split('\s*(?:%s):\s*' % '|'.join(fields), info)][1:]
        # 处理列名称
        for i in range(len(fields)):
            if '导演' == fields[i]:
                fields[i] = 'director'
            if '编剧' == fields[i]:
                fields[i] = 'scriptwriter'
            if '主演' == fields[i]:
                fields[i] = 'actors'
            if '类型' == fields[i]:
                fields[i] = 'categories'
            if '制片国家/地区' == fields[i]:
                fields[i] = 'district'
            if '语言' == fields[i]:
                fields[i] = 'language'
            if '片长' == fields[i]:
                fields[i] = 'duration'
        # 将所有信息填入item
        other_info = list(zip(fields,values))
        for field,value  in other_info:
            if field in ['IMDb链接','上映日期','官方网站','又名']:
                other_info.remove((field,value))
        final_info = dict(other_info[:-1])
        movie_item.update(final_info)
        # 处理缺失字段
        if not 'director' in movie_item.keys():
            movie_item['director'] = '/'
        if not 'scriptwriter' in movie_item.keys():
            movie_item['scriptwriter'] = '/'
        if not 'actors' in movie_item.keys():
            movie_item['actors'] = '/'
        if not 'categories' in movie_item.keys():
            movie_item['categories'] = '/'
        if not 'district' in movie_item.keys():
            movie_item['district'] = '/'
        if not 'language' in movie_item.keys():
            movie_item['language'] = '/'
        if not 'duration' in movie_item.keys():
            movie_item['duration'] = '/'
        print('~完成爬取电影: ' + movie_item['title'] + '/' + movie_item['rate'])
        #将数据加入到字典中
        yield movie_item

image.gif

四,项目总结

     爬取的数据最终会存到MYSQL服务器的表中,可以写程序将数据展示出来。注意的时会对IP进行限制封号,200条为限,超过IP会被限制,可以换一个IP进行抓去。主要研究爬虫的基本使用规范和语法,相对较为简单,供大家学习参考

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
13天前
|
数据采集 存储 API
网络爬虫与数据采集:使用Python自动化获取网页数据
【4月更文挑战第12天】本文介绍了Python网络爬虫的基础知识,包括网络爬虫概念(请求网页、解析、存储数据和处理异常)和Python常用的爬虫库requests(发送HTTP请求)与BeautifulSoup(解析HTML)。通过基本流程示例展示了如何导入库、发送请求、解析网页、提取数据、存储数据及处理异常。还提到了Python爬虫的实际应用,如获取新闻数据和商品信息。
|
17天前
|
数据采集 Python
【python】爬虫-西安医学院-校长信箱
本文以西安医学院-校长信箱为基础来展示爬虫案例。来介绍python爬虫。
【python】爬虫-西安医学院-校长信箱
|
23天前
|
数据采集 安全 Python
python并发编程:Python实现生产者消费者爬虫
python并发编程:Python实现生产者消费者爬虫
24 0
python并发编程:Python实现生产者消费者爬虫
|
3天前
|
数据采集 存储 JSON
Python爬虫面试:requests、BeautifulSoup与Scrapy详解
【4月更文挑战第19天】本文聚焦于Python爬虫面试中的核心库——requests、BeautifulSoup和Scrapy。讲解了它们的常见问题、易错点及应对策略。对于requests,强调了异常处理、代理设置和请求重试;BeautifulSoup部分提到选择器使用、动态内容处理和解析效率优化;而Scrapy则关注项目架构、数据存储和分布式爬虫。通过实例代码,帮助读者深化理解并提升面试表现。
13 0
|
6天前
|
数据采集 JavaScript 前端开发
使用Python打造爬虫程序之破茧而出:Python爬虫遭遇反爬虫机制及应对策略
【4月更文挑战第19天】本文探讨了Python爬虫应对反爬虫机制的策略。常见的反爬虫机制包括User-Agent检测、IP限制、动态加载内容、验证码验证和Cookie跟踪。应对策略包括设置合理User-Agent、使用代理IP、处理动态加载内容、验证码识别及维护Cookie。此外,还提到高级策略如降低请求频率、模拟人类行为、分布式爬虫和学习网站规则。开发者需不断学习新策略,同时遵守规则和法律法规,确保爬虫的稳定性和合法性。
|
18天前
|
数据采集 存储 前端开发
Python爬虫如何快速入门
写了几篇网络爬虫的博文后,有网友留言问Python爬虫如何入门?今天就来了解一下什么是爬虫,如何快速的上手Python爬虫。
21 0
|
23天前
|
人工智能 机器人 数据挖掘
【python】电影评分数据集的分析(python实现)(源码+报告)【独一无二】
【python】电影评分数据集的分析(python实现)(源码+报告)【独一无二】
|
23天前
|
前端开发 测试技术 数据库
【python】为什么使用python Django开发网站这么火?
【python】为什么使用python Django开发网站这么火?
|
25天前
|
机器学习/深度学习 数据采集 算法
基于Apriori关联规则的电影推荐系统(附python代码)
这是一个基于Apriori算法的电影推荐系统概览。系统通过挖掘用户评分数据来发现关联规则,例如用户观看某部电影后可能感兴趣的其他电影。算法核心是逐层生成频繁项集并设定最小支持度阈值,之后计算规则的置信度。案例中展示了数据预处理、频繁项集生成以及规则提取的过程,具体包括用户评分电影的统计分析,如1-5部电影的评分组合。最后,通过Python代码展示了Apriori算法的实现,生成推荐规则,并给出了一个简单的推荐示例。整个过程旨在提高推荐的精准度,基于用户已评分的电影推测他们可能尚未评分但可能喜欢的电影。
基于Apriori关联规则的电影推荐系统(附python代码)
|
1月前
|
数据采集 存储 Web App开发
一键实现数据采集和存储:Python爬虫、Pandas和Excel的应用技巧
一键实现数据采集和存储:Python爬虫、Pandas和Excel的应用技巧