一、项目介绍
为了充分利用网上大数据资源,让用户能够方便利用影视信息,采用基于 Scrapy 框架的爬虫技术,开发了检索电影信息的搜索引擎。对豆瓣网站的影视信息进行爬取,以方便用户准确获取最新的电影信息。
二、项目流程图
1、通用爬虫框架流程图
Screpy框架运行流程
项目代码
以“豆瓣电影”为爬取目 标,爬取网站中的影视信息。主要包括网站排名 “ Top250 ”和喜剧、动作类电影的电影名称、电影评分、电影导演, 电影上映时间以及电影评语。
首先创建工程
scrapy startproject DouBan
创建爬虫程序
cd DouBan/
scrapy genspider douban ‘douban.com’
items.py中的代码
# -*- coding: utf-8 -*-
"""
# 1. item.py文件的功能?
item.py主要目标是从非结构化来源(通常是网页)提取结构化数据。Scrapy爬虫可以将提取的数据作为Python语句返回。
# 2. 为什么使用item.py?
虽然方便和熟悉,Python dicts缺乏结构:很容易在字段名称中输入错误或返回不一致的数据,特别是在与许多爬虫的大项目。
# 3. item.py文件的优势?
- 定义公共输出数据格式,Scrapy提供Item类。
- Item对象是用于收集所抓取的数据的简单容器。
- 提供了一个类似字典的 API,具有用于声明其可用字段的方便的语法。
"""
# 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()
# rating_num = scrapy.Field()
class DouBanMovieItem(scrapy.Item):
"""
确定要爬取的数据的类型和名称,包含:
电影名称( title) ;
电影评分( score) ;
电影评语( quote) ;
电影导演( director) ,
上映日期(release_date)
评论数(comment_num)
通过 Field( ) 方法来声明数据字段。
"""
# define the fields for your item here like:
# name = scrapy.Field()
title = scrapy.Field() # 电影名称
score = scrapy.Field() # 电影评分
quote = scrapy.Field() # 电影评语
director = scrapy.Field() # 电影导演
release_date = scrapy.Field() # 上映日期
comment_num = scrapy.Field() # 评论数
image_url = scrapy.Field() # 图片的url地址
detail_url = scrapy.Field() # 电影详情页信息;
image_path = scrapy.Field() # 下載的封面本地存儲位置
middlewares.py中的重要代码
# -*- coding: utf-8 -*-
# Define here the models for your spider middleware
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html
from scrapy import signals
class DoubanSpiderMiddleware(object):
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the spider middleware does not modify the
# passed objects.
@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s
def process_spider_input(self, response, spider):
# Called for each response that goes through the spider
# middleware and into the spider.
# Should return None or raise an exception.
return None
def process_spider_output(self, response, result, spider)