原文地址: https://www.cnblogs.com/kongzhagen/p/6549053.html
Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中。其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的, 也可以应用在获取API所返回的数据(例如 Amazon Associates Web Services ) 或者通用的网络爬虫。Scrapy用途广泛,可以用于数据挖掘、监测和自动化测试。
Scrapy 使用了 Twisted异步网络库来处理网络通讯。整体架构大致如下
Scrapy主要包括了以下组件:
Scrapy运行流程大概如下:
python2.7安装
https://www.python.org/ftp/python/2.7.14/python-2.7.14.amd64.msi
示例一
一、安装
pip install scrapy
注:windows平台需要依赖pywin32,请根据自己系统32/64位选择下载安装,https://sourceforge.net/projects/pywin32/
二、爬虫举例
1、创建工程
scrapy startproject movie
2、创建爬虫程序
cd movie
scrapy genspider meiju meijutt.com
3、自动创建目录及文件
4、文件说明:
scrapy.cfg 项目的配置信息,主要为Scrapy命令行工具提供一个基础的配置信息。(真正爬虫相关的配置信息在settings.py文件中)
items.py 设置数据存储模板,用于结构化数据,如:Django的Model
pipelines 数据处理行为,如:一般结构化的数据持久化
settings.py 配置文件,如:递归的层数、并发数,延迟下载等
spiders 爬虫目录,如:创建文件,编写爬虫规则
注意:一般创建爬虫文件时,以网站域名命名
5、设置数据存储模板 items.py
import scrapy
class MovieItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
name = scrapy.Field()
6、编写爬虫 meiju.py
# -*- coding: utf-8 -*-
import scrapy
from movie.items import MovieItem
class MeijuSpider(scrapy.Spider):
name = "meiju"
allowed_domains = ["meijutt.com"]
start_urls = ['http://www.meijutt.com/new100.html']
def parse(self, response):
movies = response.xpath('//ul[@class="top-list fn-clear"]/li')
for each_movie in movies:
item = MovieItem()
item['name'] = each_movie.xpath('./h5/a/@title').extract()[0]
yield item
7、设置配置文件 settings.py增加如下内容
ITEM_PIPELINES = {'movie.pipelines.MoviePipeline':100}
8、编写数据处理脚本 pipelines.py
class MoviePipeline(object):
def process_item(self, item, spider):
with open("my_meiju.txt",'a') as fp:
fp.write(item['name'].encode("utf8") + '\n')
9、执行爬虫
cd movie
scrapy crawl meiju --nolog
10、结果
1、创建一个工程
scrapy startproject pic
2、创建爬虫程序
cd pic
scrapy genspider xh xiaohuar.com
3、自动创建目录及文件
4、文件说明:
scrapy.cfg 项目的配置信息,主要为Scrapy命令行工具提供一个基础的配置信息。(真正爬虫相关的配置信息在settings.py文件中)
items.py 设置数据存储模板,用于结构化数据,如:Django的Model
pipelines 数据处理行为,如:一般结构化的数据持久化
settings.py 配置文件,如:递归的层数、并发数,延迟下载等 spiders 爬虫目录,如:创建文件,编写爬虫规则 注意:一般创建爬虫文件时,以网站域名命名
5、设置数据存储模板 items.py
import scrapy
class PicItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
addr = scrapy.Field()
name = scrapy.Field()
6、编写爬虫 xh.py
# -*- coding: utf-8 -*-
import scrapy
import os
# 导入item中结构化数据模板
from pic.items import PicItem
class XhSpider(scrapy.Spider):
# 爬虫名称,唯一
name = "xh"
# 允许访问的域
allowed_domains = ["xiaohuar.com"]
# 初始URL
start_urls = ['http://www.xiaohuar.com/list-1-1.html']
def parse(self, response):
# 获取所有图片的a标签
allPics = response.xpath('//div[@class="img"]/a')
for pic in allPics:
# 分别处理每个图片,取出名称及地址
item = PicItem()
name = pic.xpath('./img/@alt').extract()[0]
addr = pic.xpath('./img/@src').extract()[0]
addr = 'http://www.xiaohuar.com'+addr
item['name'] = name
item['addr'] = addr
# 返回爬取到的数据
yield item
7、设置配置文件 settings.py
# 设置处理返回数据的类及执行优先级
ITEM_PIPELINES = {'pic.pipelines.PicPipeline':100}
8、编写数据处理脚本 pipelines.py
import urllib2
import os
class PicPipeline(object):
def process_item(self, item, spider):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'}
req = urllib2.Request(url=item['addr'],headers=headers)
res = urllib2.urlopen(req)
file_name = os.path.join(r'D:\my\down_pic',item['name']+'.jpg')
with open(file_name,'wb') as fp:
fp.write(res.read())
9、执行爬虫
cd pic
scrapy crawl xh --nolog
结果:
注明:基于进阶篇再修改为终极篇
# xh.py
# -*- coding: utf-8 -*-
import scrapy
import os
from scrapy.http import Request
# 导入item中结构化数据模板
from pic.items import PicItem
class XhSpider(scrapy.Spider):
# 爬虫名称,唯一
name = "xh"
# 允许访问的域
allowed_domains = ["xiaohuar.com"]
# 初始URL
start_urls = ['http://www.xiaohuar.com/hua/']
# 设置一个空集合
url_set = set()
def parse(self, response):
# 如果图片地址以http://www.xiaohuar.com/list-开头,我才取其名字及地址信息
if response.url.startswith("http://www.xiaohuar.com/list-"):
allPics = response.xpath('//div[@class="img"]/a')
for pic in allPics:
# 分别处理每个图片,取出名称及地址
item = PicItem()
name = pic.xpath('./img/@alt').extract()[0]
addr = pic.xpath('./img/@src').extract()[0]
addr = 'http://www.xiaohuar.com'+addr
item['name'] = name
item['addr'] = addr
# 返回爬取到的信息
yield item
# 获取所有的地址链接
urls = response.xpath("//a/@href").extract()
for url in urls:
# 如果地址以http://www.xiaohuar.com/list-开头且不在集合中,则获取其信息
if url.startswith("http://www.xiaohuar.com/list-"):
if url in XhSpider.url_set:
pass
else:
XhSpider.url_set.add(url)
# 回调函数默认为parse,也可以通过from scrapy.http import Request来指定回调函数
# from scrapy.http import Request
# Request(url,callback=self.parse)
yield self.make_requests_from_url(url)
else:
pass