Scrapy实战:使用scrapy再再次爬取干货集中营的妹子图片
需要学习的知识:
1.获取到的json数据如何处理
2.保存到json文件
3.保存到MongoDB数据库
4.下载项目图片(含缩略图)
1.创建项目
scrapy startproject gank
2.生成项目爬虫文件
scrapy genspider gank_img gank.io
注意:项目名称gank不能跟项目爬虫文件名gank_img一致
3.gank_img.py文件
import json
import scrapy
from gank.items import GankItem class GankImgSpider(scrapy.Spider):
name = 'gank_img'
allowed_domains = ['gank.io']
# 开始链接为什么要这样写请参考:https://www.cnblogs.com/sanduzxcvbnm/p/10271493.html
start_urls = ['https://gank.io/api/data/福利/700/1'] def parse(self, response):
# 返回的是json字符串,转换成字典,提取出需要的字段
results = json.loads(response.text)['results'] for i in results:
item = GankItem()
item['who'] = i['who']
item['url'] = i['url'] yield item
4.items.py文件
import scrapy class GankItem(scrapy.Item):
# define the fields for your item here like:
who = scrapy.Field()
url = scrapy.Field()
# 保存图片,生成图片路径
image_paths = scrapy.Field()
5.pipelines.py文件
import json
from scrapy.pipelines.images import ImagesPipeline
from scrapy.exceptions import DropItem
import pymongo
import scrapy # 在settings.py文件中开启该pipeline,则主程序中yield的数据会传输到这边来进行处理 # 保存成json文件
class JsonWriterPipeline(object): def open_spider(self, spider):
self.file = open('items.json', 'w') def close_spider(self, spider):
self.file.close() def process_item(self, item, spider):
line = json.dumps(dict(item)) + "\n"
self.file.write(line)
return item # 保存到MongoDB数据库
class MongoPipeline(object):
# 数据表名
collection_name = 'scrapy_items' def __init__(self, mongo_uri, mongo_db):
self.mongo_uri = mongo_uri
self.mongo_db = mongo_db @classmethod
# 从settings.py文件中获取参数
def from_crawler(cls, crawler):
return cls(
mongo_uri=crawler.settings.get('MONGO_URI'),
mongo_db=crawler.settings.get('MONGO_DATABASE', 'items') # 数据库名
) def open_spider(self, spider):
self.client = pymongo.MongoClient(self.mongo_uri)
self.db = self.client[self.mongo_db] def close_spider(self, spider):
self.client.close() def process_item(self, item, spider):
self.db[self.collection_name].insert_one(dict(item))
return item # 下载项目图片
class MyImagesPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
# 图片链接是https的转换成http
if item['url'][0:5] == 'https':
item['url'] = item['url'].replace(item['url'][0:5], 'http')
# for image_url in item['url']:
# print('400',image_url)
yield scrapy.Request(item['url']) 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")
item['image_paths'] = image_paths
return item
6.settings.py文件
只修改如下配置,其余保持不变
DEFAULT_REQUEST_HEADERS = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'zh-CN,en-US;q=0.8,zh;q=0.5,en;q=0.3',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0'
}
# MongoDB数据库参数
MONGO_URI = '127.0.0.1'
MONGO_DATABASE = 'gank'
ITEM_PIPELINES = {
'gank.pipelines.JsonWriterPipeline': 300,
'gank.pipelines.MyImagesPipeline': 1,
'gank.pipelines.MongoPipeline': 400,
}
# 图片保存路径
IMAGES_STORE = 'D:\\gank\\images'
# 90天的图片失效期限
IMAGES_EXPIRES = 90
# 缩略图
IMAGES_THUMBS = {
'small': (50, 50),
'big': (270, 270),
}
7.执行爬虫程序
scrapy crawl gank_img
8.效果
json文件

MongoDB数据库

保存的图片及缩略图

其中full为图片本身大小所存放目录,thubmbs为缩略图存放目录,缩略图有big和small两种尺寸



scrapy结尾会有相应的统计信息
下载图片561个,无法下载的图片有108个
为什么有的图片无法下载,请参考之前的文章:https://www.cnblogs.com/sanduzxcvbnm/p/10271493.html

Scrapy实战:使用scrapy再再次爬取干货集中营的妹子图片的更多相关文章
- Python 爬取煎蛋网妹子图片
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-08-24 10:17:28 # @Author : EnderZhou (z ...
- Scrapy实战篇(四)爬取京东商城文胸信息
创建scrapy项目 scrapy startproject jingdong 填充 item.py文件 在这里定义想要存储的字段信息 import scrapy class JingdongItem ...
- Scrapy实战篇(五)爬取京东商城文胸信息
创建scrapy项目 scrapy startproject jingdong 填充 item.py文件 在这里定义想要存储的字段信息 import scrapy class JingdongItem ...
- python爬虫–爬取煎蛋网妹子图片
前几天刚学了python网络编程,书里没什么实践项目,只好到网上找点东西做. 一直对爬虫很好奇,所以不妨从爬虫先入手吧. Python版本:3.6 这是我看的教程:Python - Jack -Cui ...
- python爬虫爬取煎蛋网妹子图片
import urllib.request import os def url_open(url): req = urllib.request.Request(url) req.add_header( ...
- Python爬取贴吧中的图片
#看到贴吧大佬在发图,准备盗一下 #只是爬取一个帖子中的图片 1.先新建一个scrapy项目 scrapy startproject TuBaEx 2.新建一个爬虫 scrapy genspider ...
- python连续爬取多个网页的图片分别保存到不同的文件夹
python连续爬取多个网页的图片分别保存到不同的文件夹 作者:vpoet mail:vpoet_sir@163.com #coding:utf-8 import urllib import ur ...
- 初识python 之 爬虫:爬取某网站的壁纸图片
用到的主要知识点:requests.get 获取网页HTMLetree.HTML 使用lxml解析器解析网页xpath 使用xpath获取网页标签信息.图片地址request.urlretrieve ...
- scrapy过滤重复数据和增量爬取
原文链接 前言 这篇笔记基于上上篇笔记的---<scrapy电影天堂实战(二)创建爬虫项目>,而这篇又涉及redis,所以又先熟悉了下redis,记录了下<redis基础笔记> ...
随机推荐
- Android开源框架ViewPageIndicator和ViewPager实现Tab导航
前言: 关于使用ViewPageIndicator和ViewPager实现Tab导航,在开发社区里已经有一堆的博客对其进行了介绍,假设我还在这里写怎样去实现.那简直就是老生常谈,毫无新奇感,并且.我也 ...
- 基于webrtc的媒体库測试代码以及接口介绍
经过一段时间的项目验证,第一版接口能够定版了.满足一般的项目需求是没有问题了,接口非常清晰,凝视也写的非常清晰,大家有须要的就拿去測试吧,android版本号还在验证中.假设有什么问题或者须要源码.能 ...
- html 转word
今日头条发表文章 python-docx — python-docx 0.8.6 documentation http://python-docx.readthedocs.io/en/latest/
- js【面向过程编程】、好、 【init()、 GetData()、 bindData()、bindDom、 bindEvent()、buyProduct()、AddProductToCart()】*****************
1. 一般页面开发方式 [可读性差.可维护性差]------初级开发工程师 一般页面编写方法 var name = 'iphone8' var description = '手机中的战斗机 ' var ...
- E20170621-hm
detroit 底特律 giant n. 巨人,大汉; 巨兽,巨物; 卓越人物 woo vt. 求爱,求婚; 争取…的支持; convince vt. 使相信,说服,使承认; 使明白; ...
- km算法(二分图最大权匹配)学习
啦啦啦! KM算法是通过给每个顶点一个标号(叫做顶标)来把求最大权匹配的问题转 化为求完备匹配的问题的.设顶点Xi的顶标为A[i],顶点Yi的顶标为B[i],顶点Xi与Yj之间的边权为w[i,j].在 ...
- P2924 [USACO08DEC]大栅栏Largest Fence
传送门 反正我是看不出来这题和凸包有什么关系--大佬们是怎么想到的-- 不准确一点的说,本题就是要我们求一个边上点数最多的凸包 我们可以先把所有的边都取出来,然后按极角排序.枚举这个凸包的起点,然后做 ...
- Web开发必须知道的知识点
Web前端必须知道 一.常用那几种浏览器测试.有哪些内核(Layout Engine) 1.浏览器:IE,Chrome,FireFox,Safari,Opera. 2.内核:Trident,Gecko ...
- hdu2030
http://acm.hdu.edu.cn/showproblem.php?pid=2030 #include<stdio.h> #include<math.h> #inclu ...
- IOS开发之Swift学习笔记
1.因为存储属性要求初始化,我们可以使用lazy修饰符来延迟初始化.