前一段时间做过一些爬虫的小项目,用的都是urllib基础库,后来听说用scrapy做爬虫效率高。经过一段时间的学习,做了一个基于scrapy框架的爬虫项目,爬取图片并且保存在本地。
首先在item.py文件中定义我们要获取的内容:
# -*- coding: utf-8 -*-
# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html
import scrapy
class LyfItem(scrapy.Item):
link_url = scrapy.Field()
img_url = scrapy.Field() # 图片链接
image_paths = scrapy.Field() # 图片保存地址
接下来在scrapy.py文件中写scrapy框架最重要的部分——如何爬取了。为了爬取大量的图片,我选择去昵图网,很大的原因就是这种网站反扒机制没那么严格,对于我这种新手再适合不过了
![](https://i-blog.csdnimg.cn/blog_migrate/06d2599173ae00886ceb218a755148cd.png)