爬虫框架Scrapy之案例三图片下载器
items.py
class CoserItem(scrapy.Item):
url = scrapy.Field()
name = scrapy.Field()
info = scrapy.Field()
image_urls = scrapy.Field()
images = scrapy.Field()
spiders/coser.py
# -*- coding: utf-8 -*-
from scrapy.selector import Selector
import scrapy
from scrapy.contrib.loader import ItemLoader
from Cosplay.items import CoserItem
class CoserSpider(scrapy.Spider):
name = "coser"
allowed_domains = ["bcy.net"]
start_urls = (
'http://bcy.net/cn125101',
'http://bcy.net/cn126487',
'http://bcy.net/cn126173'
)
def parse(self, response):
sel = Selector(response)
for link in sel.xpath("//ul[@class='js-articles l-works']/li[@class='l-work--big']/article[@class='work work--second-created']/h2[@class='work__title']/a/@href").extract():
link = 'http://bcy.net%s' % link
request = scrapy.Request(link, callback=self.parse_item)
yield request
def parse_item(self, response):
l = ItemLoader(item=CoserItem(), response=response)
l.add_xpath('name', "//h1[@class='js-post-title']/text()")
l.add_xpath('info', "//div[@class='post__info']/div[@class='post__type post__info-group']/span/text()")
urls = l.get_xpath('//img[@class="detail_std detail_clickable"]/@src')
urls = [url.replace('/w650', '') for url in urls]
l.add_value('image_urls', urls)
l.add_value('url', response.url)
return l.load_item()
pipelines.py
import requests
from Cosplay import settings
import os
class ImageDownloadPipeline(object):
def process_item(self, item, spider):
if 'image_urls' in item:
images = []
dir_path = '%s/%s' % (settings.IMAGES_STORE, spider.name)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
for image_url in item['image_urls']:
us = image_url.split('/')[3:]
image_file_name = '_'.join(us)
file_path = '%s/%s' % (dir_path, image_file_name)
images.append(file_path)
if os.path.exists(file_path):
continue
with open(file_path, 'wb') as handle:
response = requests.get(image_url, stream=True)
for block in response.iter_content(1024):
if not block:
break
handle.write(block)
item['images'] = images
return item
settings.py
ITEM_PIPELINES = {'Cosplay.pipelines.ImageDownloadPipeline': 1}
IMAGES_STORE = '../Images'
DOWNLOAD_DELAY = 0.25 # 250 ms of delay
在项目根目录下新建main.py文件,用于调试
from scrapy import cmdline
cmdline.execute('scrapy crawl coser'.split())
执行程序
py2 main.py
爬虫框架Scrapy之案例三图片下载器的更多相关文章
- Python爬虫框架Scrapy实例(三)数据存储到MongoDB
Python爬虫框架Scrapy实例(三)数据存储到MongoDB任务目标:爬取豆瓣电影top250,将数据存储到MongoDB中. items.py文件复制代码# -*- coding: utf-8 ...
- Python爬虫框架Scrapy实例(四)下载中间件设置
还是豆瓣top250爬虫的例子,添加下载中间件,主要是设置动态Uesr-Agent和代理IP Scrapy代理IP.Uesr-Agent的切换都是通过DOWNLOADER_MIDDLEWARES进行控 ...
- 爬虫框架Scrapy之案例二
新浪网分类资讯爬虫 爬取新浪网导航页所有下所有大类.小类.小类里的子链接,以及子链接页面的新闻内容. 效果演示图: items.py import scrapy import sys reload(s ...
- 爬虫框架Scrapy之案例一
阳光热线问政平台 http://wz.sun0769.com/index.php/question/questionType?type=4 爬取投诉帖子的编号.帖子的url.帖子的标题,和帖子里的内容 ...
- 第三百四十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—编写spiders爬虫文件循环抓取内容—meta属性返回指定值给回调函数—Scrapy内置图片下载器
第三百四十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—编写spiders爬虫文件循环抓取内容—meta属性返回指定值给回调函数—Scrapy内置图片下载器 编写spiders爬虫文件循环 ...
- 二十 Python分布式爬虫打造搜索引擎Scrapy精讲—编写spiders爬虫文件循环抓取内容—meta属性返回指定值给回调函数—Scrapy内置图片下载器
编写spiders爬虫文件循环抓取内容 Request()方法,将指定的url地址添加到下载器下载页面,两个必须参数, 参数: url='url' callback=页面处理函数 使用时需要yield ...
- 第三篇:爬虫框架 - Scrapy
前言 Python提供了一个比较实用的爬虫框架 - Scrapy.在这个框架下只要定制好指定的几个模块,就能实现一个爬虫. 本文将讲解Scrapy框架的基本体系结构,以及使用这个框架定制爬虫的具体步骤 ...
- 小白学 Python 爬虫(35):爬虫框架 Scrapy 入门基础(三) Selector 选择器
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http://www.xiaohuar.com/,让你体验爬取校花的成就感. Scr ...
随机推荐
- Boss Group Worker Group NioEventLoopGroup
宜人贷蜂巢API网关技术解密之Netty使用实践 - honeycomb2017的博客 - CSDN博客 https://blog.csdn.net/honeycomb2017/article/det ...
- bash characters
linux shell通配符(wildcard) 通配符是由shell处理的(不是由所涉及到命令语句处理的,其实我们在shell各个命令中也没有发现有这些通配符介绍), 它只会出现在 命令的“参数”里 ...
- 网络模型一般是指 OSI 七层参考模型和 TCP/IP 五层参考模型。
网络模型一般是指 OSI 七层参考模型和 TCP/IP 五层参考模型. 每一层实现各自的功能和协议,并且都为上一层提供业务功能.为了提供这 种业务功能,下一层将上一层中的数据并入到本层的数据域中,然后 ...
- PHP 创建中文目录的情况
因为一个作业需要创建一些中文的目录,其实主要还是考虑一下编码问题. 首先确认下系统环境是什么编码,如果是gbk或者GB2312那就需要转下码,还有些特殊字符,就需要有个特殊的写法. iconv('UT ...
- 模拟hadoop-rpc通信
一.RPC服务类 package com.css.rpc.server; import java.io.IOException; import org.apache.hadoop.HadoopIlle ...
- 使用python的logging模块(转)
一.从一个使用场景开始 开发一个日志系统, 既要把日志输出到控制台, 还要写入日志文件 import logging # 创建一个logger logger = logging.getLogger(' ...
- 爬虫之UserAgent
UserAgent简介 UserAgent中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,UserAgent也简称UA.它是一个特殊字符串头,是一种向访问网站提供你所使用的浏览器类型及 ...
- django 模板语言之 simple_tag 自定义模板
自定义函数 simple_tag a. app项目下创建templatetags目录 b. 创建任意xxoo.py文件 用做自定义py函数 c. 创建template对象 register 在函数或者 ...
- 利用ssh的私钥登录Linux server
[https://blog.csdn.net/hello0370/article/details/41650513] 1.创建一个private key的文件,我使用vim编辑器,创建vi my_pr ...
- Python(^^^^^小技巧^^^^^——不定期更新)
偶然想到的小技巧 ''' 交互中对传入函数的参数的数目进行检测 ''' def func(a,b,c): print(a,b,c) s=input(">>>>:&qu ...