学习自:Python Scrapy 爬虫框架实例(一) - Blue·Sky - 博客园

这一节是对前两节内容的补充,涉及内容为一些额外的类与方法,来对原代码进行改进

原代码:这里并没有用前两节的代码,而是用了另一个爬虫的代码,作用是爬取千图网的图片信息。该爬虫的基本信息:

项目名:AdilCrawler

爬虫名:thousandPic

网址:www.58pic.com

开始爬取的网址:https://www.58pic.com/c/

Item类:AdilcrawlerItem

xpath表达式:

Author:/html/body/div[4]/div[3]/div/a/p[2]/span/span[2]/text()
Name:/html/body/div[4]/div[3]/div/a/p[1]/span[1]/text()
# -*- coding: utf-8 -*-
import scrapy
# 这里使用 import 或是 下面from 的方式都行,关键要看 当前项目在pycharm的打开方式,是否是作为一个项目打开的,建议使用这一种方式。
import AdilCrawler.items as items class ThousandpicSpider(scrapy.Spider):
name = 'thousandPic'
allowed_domains = ['www.58pic.com']
start_urls = ['https://www.58pic.com/c/'] def parse(self, response): item = items.AdilcrawlerItem()
author = response.xpath('/html/body/div[4]/div[3]/div/a/p[2]/span/span[2]/text()').extract()
theme = response.xpath('/html/body/div[4]/div[3]/div/a/p[1]/span[1]/text()').extract()
item['author'] = author
item['theme'] = theme
return item

补充内容

1、日志打印,写在parse方法下:

def parse(self,response):
...
self.log(author)
...

2、ItemLoader类:代替extract()、xpath()方法

1)从scrapy.loader导入ItemLoader

from scrapy.loader import ItemLoader

2)优化,写在spider.py文件

用add_xpath方法,代替XPath提取语句:xpath(xxx).extract()

import scrapy
from AdilCrawler.items import AdilcrawlerItem
from scrapy.loader import ItemLoader class ThousandpicoptimizeSpider(scrapy.Spider):
name = 'thousandPicOptimize'
allowed_domains = ['www.58pic.com']
start_urls = ['http://www.58pic.com/c/'] def parse(self, response):
i = ItemLoader(item = AdilcrawlerItem(),response = response )
i.add_xpath('author','/html/body/div[4]/div[3]/div/a/p[2]/span/span[2]/text()')
i.add_xpath('theme','/html/body/div[4]/div[3]/div/a/p[1]/span[1]/text()')
return i.load_item()

相当于:

        i = items.AdilcrawerItem()
author = response.xpath('/html/body/div[4]/div[3]/div/a/p[2]/span/span[2]/text()').extract()
theme = response.xpath('/html/body/div[4]/div[3]/div/a/p[1]/span[1]/text()').extract()
i['author'] = author
i['theme'] = theme
yield i

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

i = ItemLoader(item = AdilcrawlerItem(),response = response )
     i.add_xpath('author','/html/body/div[4]/div[3]/div/a/p[2]/span/span[2]/text()')
     i.add_xpath('theme','/html/body/div[4]/div[3]/div/a/p[1]/span[1]/text()')
yield i.load_item()

3)修改pipelines.py,目的是将爬取到的内容保存到文件中;

Spider中爬取到的内容都会以参数item的形式传入pipelines.py文件中的相关方法,比如process_item,(这一点很重要,且对所有类型的爬虫都适用)

import json

class AdilcrawlerPipeline(object):
def __init__(self):
self.filename = open('thousandPic.json','w')
def process_item(self, item, spider):
# ensure_ascii=False 可以解决 json 文件中 乱码的问题。
text = json.dumps(dict(item), ensure_ascii=False) + ',\n' # 这里是一个字典一个字典存储的,后面加个 ',\n' 以便分隔和换行。
self.filename.write(text)
    return item
def close_spider(self,spider):
self.filename.close()

至于json序列化保存的方法,可以参考序列化 pickle JSON - ShineLe - 博客园

4)修改settings.py

ITEM_PIPELINES = {
'AdilCrawler.pipelines.AdilcrawlerPipeline': 300,
}
# 加入后,相当于开启pipeline,此时在执行爬虫,会执行对应的pipelines下的类,并执行该类相关的方法,比如这里上面的保存数据功能。

5)执行;由于在pipelines.py中已经写了文件保存方法,故此处不用-o参数输出为文件

scrapy crawl thousandPicOptimize

3、CrawlSpider类:翻页抓取

1)使用crawl模板创建一个CrawlSpider PicSpi(CrawlSpider的生成,与普通Spider不同)

scrapy genspider -t crawl PicSpi www.58pic.com

2)items.py:与之前相同;

import scrapy
class PicsItem(scrapy.Item):
authod=scrapy.Field() #作者
theme=scrapy.Field() #主题

pipelines.py同2

3)Spider文件PicSpi.py

需要用到另外三个类:LinkExtractor、CrawlSpider、Rule

from scrapy.linkextractors import LinkExtractor
from scrapy.spider import CrawlSpider,Rule

完整代码:

# 导入链接规则匹配类,用来提取符合规则的链接
from scrapy.linkextractors import LinkExtractor
# 导入CrawlSpider类和Rule              (①)
from scrapy.spiders import CrawlSpider, Rule
import AdilCrawler.items as items class ThousandpicpagingSpider(CrawlSpider):
name = 'thousandPicPaging'
allowed_domains = ['www.58pic.com']
start_urls = ['https://www.58pic.com/c/'] # Response中的链接提取规则,返回符合规则的链接匹配对象的List
# 根据翻页链接地址 http://www.58pic.com/c/1-0-0-03.html 找到相应的正则表达式
# 不能使用restrict_xpath,否则正则将失效
   #(②)
page_link = LinkExtractor(allow='https://www.58pic.com/c/\S-\S-\S-\S\S.html', allow_domains='www.58pic.com')
   #(③)
rules = (
Rule(page_link, callback='parse_item', follow=True), # 此处的 ','不能省略
) # 方法parse_start_url是为了解决parse_item()不能抓取第一页数据的问题
# 原因是第一页的正则表达式形式与之后若干页不同
# 该方法是CrawlSpider类下的方法,这里重写一下即可
  #(④)
def parse_start_url(self, response):
i = items.AdilcrawerItem()
author = response.xpath('/html/body/div[4]/div[3]/div/a/p[2]/span/span[2]/text()').extract()
theme = response.xpath('/html/body/div[4]/div[3]/div/a/p[1]/span[1]/text()').extract()
i['author'] = author
i['theme'] = theme
yield i
  #(⑤)
def parse_item(self, response):
i = items.AdilcrawerItem()
author = response.xpath('/html/body/div[4]/div[3]/div/a/p[2]/span/span[2]/text()').extract()
theme = response.xpath('/html/body/div[4]/div[3]/div/a/p[1]/span[1]/text()').extract()
i['author'] = author
i['theme'] = theme
yield i

对这段代码的说明:

①:库的导入,这一部分的内容是在通过指令生成CrawlSpider的时候自动生成的

②:得到每一页的链接

page_link = LinkExtractor(allow='https://www.58pic.com/c/\S-\S-\S-\S\S.html', allow_domains='www.58pic.com')

LInkExtractor构建链接时,需要参数allow与allow_domains,参数allow是各页URL的完整正则表达式,allow_domains是限定访问的范围;这样page_link便保存了访问每一页的方法。

③:根据每页链接构建访问规则

    rules = (
Rule(page_link, callback='parse_item', follow=True), # 此处的 ','不能省略
)

a、用Rule方法,参数有三项:page_link、callback、follow,作用分别是:

page_link:②中构建的链接,由于是正则表达式,所以是一系列的链接

callback:对这些链接进行处理的函数,需要在之后补充在同一个类中

follow:是否根据链接自动进行下去

b、最后,必须加,逗号,以表明这是一个Tuple元组

④:访问第一页(针对第一页URL不与正则表达式相匹配的情况):parse_start_url

⑤:访问其他页(代码相同,只是方法名不同):parse_item

        i = items.AdilcrawerItem()
author = response.xpath('/html/body/div[4]/div[3]/div/a/p[2]/span/span[2]/text()').extract()
theme = response.xpath('/html/body/div[4]/div[3]/div/a/p[1]/span[1]/text()').extract()
i['author'] = author
i['theme'] = theme
yield i

代码内容与普通情况下的要素提取相同

4)运行;运行方法倒是相同

scrapy crawl thousandPicPaging

4、综合,将2、3结合起来,同时使用ItemLoader、CrawlSpider

import scrapy
from scrapy.loader import ItemLoader
from scrapy.spiders import CrawlSpider,Rule
import AdilCrawler.items as items class ThousandpicpagingopSpider(CrawlSpider):
name='thousandPicPagingOp'
allowed_domains = ['www.58pic.com']
start_urls = ['http://www.58pic.com/c/'] page_link=LinkExtractor(allow='http://www.58pic.com/c/\S-\S-\S-\S\S.html',allow_domains='www.58pic.com')
rules=(
Rule(page_link,callback='parse_item',follow=True),
) def parse_start_url(self,response):
i=ItemLoader(item=items.AdilcrawlerItem(),response=response)
i.add_xpath('author','/html/body/div[4]/div[3]/div/a/p[2]/span/span[2]/text()')
     i.add_xpath('theme','/html/body/div[4]/div[3]/div/a/p[1]/span[1]/text()')
yield i.load_item() def parse_item(self, response):
i = ItemLoader(item = items.AdilcrawlerItem(),response = response )
i.add_xpath('author','/html/body/div[4]/div[3]/div/a/p[2]/span/span[2]/text()')
i.add_xpath('theme','/html/body/div[4]/div[3]/div/a/p[1]/span[1]/text()') yield i.load_item()

5、总结

用CrawlSpider和ItemLoader写一个可以自动翻页的爬虫的一般流程及文件配置。

1)创建项目与爬虫

scrapy startproject XXX
scrapy genspider -t crawl xxxS url

2)items.py

import scrapy

class xxxItem(scrapy.Item):
attr1=scrapy.Field()
attr2=scrapy.Field()
...
attrn=scrapy.Field()

3)pipelines.py

import json

class xxxPipeline(object):
def __init__(self):
self.filename = open('xxx.json','w') #保存文件 def process_item(self,item,spider):
# ensure_ascii=False 可以解决 json 文件中 乱码的问题。
text = json.dumps(dict(item), ensure_ascii=False)
self.filename.write(text)
return item
def close_spider(self,spider):
self.filename.close()

4)settings.py

BOT_NAME =
SPIDER_MODULES =
NEWSPIDER_MODULE =
ROBOTSTXT_OBEY = False
ITEM_PIPELINES = {
'XXX.pipelines.xxxPipeline': 300,
}

5)spider.py

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.loader import ItemLoader
from scrapy.spiders import CrawlSpider, Rule
import XXX.items as items class XXXSpider(CrawlSpider):
name = 'xxxS'
allowed_domains = ['url']
start_urls = ['url'] page_link = LinkExtractor(allow='url regex', allow_domains='url')
rules = (
Rule(page_link, callback='parse_item', follow=True),
) #处理首页
def parse_start_url(self, response):
i = ItemLoader(item = items.XXXItem(),response = response )
i.add_xpath('attr1','attr1 xpath')
i.add_xpath('attr2','attr2 xpath')
yield i.load_item() # 处理其他页
def parse_item(self, response):
i = ItemLoader(item = items.XXXItem(),response = response )
i.add_xpath('attr1','attr1 xpath')
i.add_xpath('attr2','attr2 xpath')
yield i.load_item()

6)运行爬虫

scrapy crawl xxxS

Python:Scrapy(三) 进阶:额外的一些类ItemLoader与CrawlSpider,使用原理及总结的更多相关文章

  1. python进阶01 面向对象、类、实例、属性封装、实例方法

    python进阶01 面向对象.类.实例.属性封装.实例方法 一.面向对象 1.什么是对象 #一切皆对象,可以简单地将“对象”理解为“某个东西” #“对象”之所以称之为对象,是因为它具有属于它自己的“ ...

  2. Python进阶开发之元类编程

    系列文章 √第一章 元类编程,已完成 ; 本文目录 类是如何产生的如何使用type创建类理解什么是元类使用元类的意义元类实战:ORM . 类是如何产生的 类是如何产生?这个问题肯定很傻.实则不然,很多 ...

  3. 孤荷凌寒自学python第三十四天python的文件操作对file类的对象学习

     孤荷凌寒自学python第三十四天python的文件操作对file类的对象学习 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.close() 当一个file对象执行此方法时,将关闭当前 ...

  4. python scrapy 抓取脚本之家文章(scrapy 入门使用简介)

    老早之前就听说过python的scrapy.这是一个分布式爬虫的框架,可以让你轻松写出高性能的分布式异步爬虫.使用框架的最大好处当然就是不同重复造轮子了,因为有很多东西框架当中都有了,直接拿过来使用就 ...

  5. python基础——面向对象进阶下

    python基础--面向对象进阶下 1 __setitem__,__getitem,__delitem__ 把对象操作属性模拟成字典的格式 想对比__getattr__(), __setattr__( ...

  6. 【转】Python之函数进阶

    [转]Python之函数进阶 本节内容 上一篇中介绍了Python中函数的定义.函数的调用.函数的参数以及变量的作用域等内容,现在来说下函数的一些高级特性: 递归函数 嵌套函数与闭包 匿名函数 高阶函 ...

  7. python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解

     1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...

  8. python Scrapy安装和介绍

    python Scrapy安装和介绍 Windows7下安装1.执行easy_install Scrapy Centos6.5下安装 1.库文件安装yum install libxslt-devel ...

  9. python 面向对象终极进阶之开发流程

    好了,你现在会了面向对象的各种语法了,  但是你会发现很多同学都是学会了面向对象的语法,却依然写不出面向对象的程序,原因是什么呢?原因就是因为你还没掌握一门面向对象设计利器, 此刻有经验的人可能会想到 ...

随机推荐

  1. MySQL存储引擎(最全面的概括)

    目录 一:MySQL存储引擎 1.什么是存储引擎? 2.查看存储引擎信息 二:MySQL支持的存储引擎 1.存储引擎 三:innoDB存储引擎 1.特性 2.存储结构 3.优缺点.适用场景 四:MyI ...

  2. SpringDataRedis入门到深入

    一:简介 SpringDataRedis是SpringData开源项目中的一部分,它可以在Spring项目中更灵活简便的访问和操作Redis:原先在没有SpringDataRedis时往往使用Jedi ...

  3. 计算机网络再次整理————socket[一]

    前言 以前也整理过吧,写了几篇之后,感觉没啥整理的必要了然后就放弃了,最近又想整理一下. 正文 这篇对应的是:https://www.cnblogs.com/aoximin/p/12235333.ht ...

  4. Sweetalert模态对话框与Swiper轮播插件、Bootstrap样式组件、AdminLTE后台管理模板地址

    Sweetalert纯JS模态对话框插件地址:http://mishengqiang.com/sweetalert/ AdminLTE后台管理模板系统地址(基于Bootstrap):https://a ...

  5. kibana 对es的简单操作。

    一.查询和查看. #1.查询所有的数据 GET _search { "query": { "match_all":{} } } #2. 查看ES集群的健康状态 ...

  6. Maven 警告 expected START_TAG or END_TAG not TEXT

    原因 在Maven警告提示区域存在空格等不规范字符,在网上复制到项目中时经常出现类似问题. pop.xml文件,setting.xml文件极易出现此类问题. 解决 将空格删除,规范一下格式就好了. 示 ...

  7. Redis 哨兵模式

    主从切换技术的方法是:当主服务器宕机了,需要手动将一台从服务器切换为主服务器,这就需要人工干预,这可能会造成一段时间的服务不可用. 一.哨兵模式的概述: 哨兵是一个独立的进程,作为一个进程,他会独立地 ...

  8. linux+nginx+tomcat负载均衡,实现session同步

    第一部分:nginx反向代理tomcat 一.软件及环境 软件 系统 角色 用途 安装的软件 ip地址 Centos6.5x86_64 nginx 反向代理用户请求 nginx 172.16.249. ...

  9. 「 题解 」P2487 [SDOI2011]拦截导弹

    简单题意 给定 \(n\) 个数对 \((h_i, v_i)\). 求: 最长不上升子序列的长度. 对于每个 \(i\),分别求出包含数对 \((h_i, v_i)\) 的最长上升子序列的个数和最长不 ...

  10. 系统操作命令实践 下(系统指令+增删改查+vim编辑器)

    目录 1.考试 2.今日问题 3.今日内容 4.复制文件 4.移动文件 Linux文件查看补充 cat , nl 5.删除文件 6.系统别名 7.vi/vim编辑器 系统操作命令实践 下(系统指令+增 ...