初识scrapy,美空网图片爬取实战
- 创建一个scrapy project
- 定义需要从网页中提取的元素item
- 实现一个spider类,通过接口完成爬取url和提取item的功能
- 实现一个item pipeline类,完成Item的存储功能。
定义Item 在items.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 MokoItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
url = scrapy.Field()
- 这里的url用来存储最后结果的dict号,稍后会继续解释,名字是随意命名的。比如说我还需要爬图片作者的名字,那么我们就可以添加一项name = scrapy.Field(),依次类推。
- 接下来我们进入spiders文件夹,里面建立一个python文件,名字咱这里就取mokospider.py,添加核心代码实现Spider:
Spider是一个继承自scrapy.contrib.spiders.CrawlSpider的Python类,有三个必需的定义的成员
name: 名字,这个spider的标识,必须是唯一的,不同的爬虫定义不同的名字
start_urls:一个url列表,spider从这些网页开始抓取
parse():解析的方法,调用的时候传入从每一个URL传回的Response对象作为唯一参数,负责解析并匹配抓取的数据(解析为item),跟踪更多的URL。
# -*- coding: utf-8 -*-
#File name :spyders/mokospider.py
#Author:Jhonny Zhang
#mail:veinyy@163.com
#create Time : 2014-11-29
############################################################################# from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from moko.items import MokoItem
import re
from scrapy.http import Request
from scrapy.selector import Selector class MokoSpider(CrawlSpider):
name = "moko"
allowed_domains = ["moko.cc"]
start_urls=["http://www.moko.cc/post/aaronsky/list.html"]
rules = (Rule(SgmlLinkExtractor(allow=('/post/\d*\.html')), callback = 'parse_img', follow=True),)
def parse_img(self, response):
urlItem = MokoItem()
sel = Selector(response)
for divs in sel.xpath('//div[@class="pic dBd"]'):
img_url=divs.xpath('.//img/@src2').extract()[0]
urlItem['url'] = img_url
yield urlItem

from moko.items import MokoItem class MokoPipeline(object):
def __init__(self):
self.mfile = open('test.html', 'w')
def process_item(self, item, spider):
text = '<img src="' + item['url'] + '" alt = "" />'
self.mfile.writelines(text)
def close_spider(self, spider):
self.mfile.close()
BOT_NAME = 'moko' SPIDER_MODULES = ['moko.spiders']
NEWSPIDER_MODULE = 'moko.spiders' # Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'moko (+http://www.yourdomain.com)' ITEM_PIPELINES={'moko.pipelines.MokoPipeline': 1,}

初识scrapy,美空网图片爬取实战的更多相关文章
- Python爬虫入门教程 3-100 美空网数据爬取
美空网数据----简介 从今天开始,我们尝试用2篇博客的内容量,搞定一个网站叫做"美空网"网址为:http://www.moko.cc/, 这个网站我分析了一下,我们要爬取的图片在 ...
- Requests 校花网图片爬取
纪念我们闹过的矛盾,只想平淡如水 import requestsimport reurl = 'http://www.xiaohuar.com/list-1-%s.html'for i in rang ...
- python爬虫调用搜索引擎及图片爬取实战
实战三-向搜索引擎提交搜索请求 关键点:利用搜索引擎提供的接口 百度的接口:wd="要搜索的内容" 360的接口:q="要搜索的内容" 所以我们只要把我们提交给 ...
- Scrapy教程--豆瓣电影图片爬取
一.先上效果 二.安装Scrapy和使用 官方网址:https://scrapy.org/. 安装命令:pip install Scrapy 安装完成,使用默认模板新建一个项目,命令:scrapy s ...
- 爬虫实战--基于requests和beautifulsoup的妹子网图片爬取(福利哦!)
#coding=utf-8 import requests from bs4 import BeautifulSoup import os all_url = 'http://www.mzitu.co ...
- Python爬虫入门教程 4-100 美空网未登录图片爬取
美空网未登录图片----简介 上一篇写的时间有点长了,接下来继续把美空网的爬虫写完,这套教程中编写的爬虫在实际的工作中可能并不能给你增加多少有价值的技术点,因为它只是一套入门的教程,老鸟你自动绕过就可 ...
- scrapy之360图片爬取
#今日目标 **scrapy之360图片爬取** 今天要爬取的是360美女图片,首先分析页面得知网页是动态加载,故需要先找到网页链接规律, 然后调用ImagesPipeline类实现图片爬取 *代码实 ...
- 爬虫07 /scrapy图片爬取、中间件、selenium在scrapy中的应用、CrawlSpider、分布式、增量式
爬虫07 /scrapy图片爬取.中间件.selenium在scrapy中的应用.CrawlSpider.分布式.增量式 目录 爬虫07 /scrapy图片爬取.中间件.selenium在scrapy ...
- [Python_scrapy图片爬取下载]
welcome to myblog Dome地址 爬取某个车站的图片 item.py 中 1.申明item 的fields class PhotoItem(scrapy.Item): # define ...
随机推荐
- css pre如果同时运用了css的border-radius、 overflow两个属性且标签中内容太多时,外部div滚动条在firefox下滚动时很卡
pre如果同时运用了css的border-radius. overflow两个属性且标签中内容太多时,外部div滚动条在firefox下滚动时很卡. 解决方法:去掉css中border-radius. ...
- export-data.js
var timeBtnClick = (function() { function _todayClick() { $('.select-time .today').on('click', funct ...
- openssl 第一篇
自从老罗赞助了openssl以及心脏出血事件的新闻,得以了解了openssl.那么什么是openssl呢?下面摘自官网: The OpenSSL Project is a collaborative ...
- 掌握string.h里的常用函数
字符串输出函数 puts 格式: puts(字符数组名) 功能:把字符数组中的字符串输出到显示器. 即在屏幕上显示该字符串. 字符串输入函数 gets 格式: gets (字符数组名) 功能:从标 ...
- showModalDialog(转)
基本介绍: showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) ...
- NOI十连测 第四测 T2
思路:线段树套可持久化treap,可持久化treap我还是第一次听说.. 改题的时候没看数据范围..乱开数组T_T #include<algorithm> #include<cstd ...
- (续)一个demo弄清楚位图在内存中的存储结构
本来续---数字图像处理之位图在计算机中的存储结构一文,通过参考别人的代码,进行修改和测试终于成功运行. 该实例未使用任何API和相关类,相信如果对此实例能够完全理解那么将有进一步进行数字图像处理的能 ...
- Cuckoo hash算法分析
一 基本思想: cuckoo hash是一种解决hash冲突的方法,其目的是使用简单的hash 函数来提高hash table的利用率,同时保证O(1)的查询时间 基本思想是使用2个hash函数来处理 ...
- Unity 编辑器扩展自定义窗体
这次看见Unity还可以自定义弹出窗体,让我很好奇.于是就去网上找文章看了看. 如果想自定义窗体需要把类放入Editor文件夹下面. 代码如下: using UnityEngine; using Un ...
- Unity 4.6 uGUI的点击事件
因为Unity 4.6刚刚发布,自带的uGUI功能的相关资料还不是很完善,今天刚装的Unity 4.6,想看一下uGUI是否好用,那么开始就今天的学习吧啊! 1,新建一个空的工程.