CrawlSpider 全站数据爬取

创建 crawlSpider 爬虫文件

  • scrapy genspider -t crawl chouti www.xxx.com

    import scrapy
    from scrapy.linkextractors import LinkExtractor
    from scrapy.spiders import CrawlSpider, Rule class CrawSpider(CrawlSpider):
    name = 'craw'
    # allowed_domains = ['www.xxx.com']
    start_urls = ['https://dig.***.com/r/scoff/hot/1']
    #连接提取器:可以根据指定条件提取连接
    link = LinkExtractor(allow=r'/r/scoff/hot/\d+')
    # link1 = LinkExtractor(allow=r'/pic/$') 针对于第一页的 url 不同的 页面使用 rules = (
    #规则解析器:将连接提取器提取到的连接对应的页面进行指定规则的数据解析
    Rule(link, callback='parse_item', follow=True),
    #参数follow=True:将连接提取器继续作用到连接提取器提取到的连接所有对应的页面中
    # Rule(link1, callback='parse_item', follow=False),
    ) def parse_item(self, response):
    print(response)
    对于简介与详情不是一个 item 的存储
    # -*- coding: utf-8 -*-
    import scrapy
    from scrapy.linkextractors import LinkExtractor
    from scrapy.spiders import CrawlSpider, Rule from tenPro.items import TenproItem, TenproItem_detail class TenSpider(CrawlSpider):
    name = 'ten'
    # allowed_domains = ['www.ccc.com']
    start_urls = ['https://hr.****.com/position.php?&start=#a0']
    rules = (
    Rule(LinkExtractor(allow=r'&start=\d+#a'), callback='parse_item', follow=True),
    Rule(LinkExtractor(allow=r'position_detail.php\?id ='), callback='parse_detail', follow=True),
    ) def parse_item(self, response):
    # 岗位名称和类别
    tr_list = response.xpath(
    '//table[@class="tablelist"]/tr[@class="odd"] | //table[@class="tablelist"]/tr[@class="even"]')
    for tr in tr_list:
    title = tr.xpath('./td[1]/a/text()').extract_first()
    kind = tr.xpath('./td[2]/text()').extract_first()
    item = TenproItem()
    item['title'] = title
    item['kind'] = kind
    yield item def parse_detail(self, response):
    desc = response.xpath('//ul[@class="squareli"]//text()').extract()
    desc = ''.join(desc)
    item = TenproItem_detail()
    item['desc'] = desc yield item
    import scrapy
    
    class TenproItem(scrapy.Item):
    # define the fields for your item here like:
    title = scrapy.Field()
    kind = scrapy.Field()
    # pass
    class TenproItem_detail(scrapy.Item):
    desc = scrapy.Field()
    # 分别进行存储  利用数据库的 多表联查  或数据解析
    class TenproPipeline(object):
    def process_item(self, item, spider):
    desc = None
    if item.__class__.__name__ == 'TenproItem_detail':
    desc = item['desc']
    else:
    title = item['title']
    kind = item['kind']
    print(item)
    return item

    思路:

    基于手动请求发送的形式:对所有页面表示的url发起请求,获取页面数据,进行解析

    基于CrawlSpider的形式:使用链接提取器和规则解析器进行所有页面对应页面数据的获取也指定数据的解析

Scrapy 框架 CrawlSpider 全站数据爬取的更多相关文章

  1. 基于Scrapt框架的全站数据爬取

    创建scrapy工程项目,除了爬虫文件中的代码需要略微修改,其他模块用法相同(如中间件,管道等): 爬虫文件代码流程 导入链接提取器 from scrapy.linkextractors import ...

  2. scrapy框架之CrawlSpider全站自动爬取

    全站数据爬取的方式 1.通过递归的方式进行深度和广度爬取全站数据,可参考相关博文(全站图片爬取),手动借助scrapy.Request模块发起请求. 2.对于一定规则网站的全站数据爬取,可以使用Cra ...

  3. scrapy框架基于CrawlSpider的全站数据爬取

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

  4. python框架Scrapy中crawlSpider的使用——爬取内容写进MySQL

    一.先在MySQL中创建test数据库,和相应的site数据表 二.创建Scrapy工程 #scrapy startproject 工程名 scrapy startproject demo4 三.进入 ...

  5. 爬虫(十七):Scrapy框架(四) 对接selenium爬取京东商品数据

    1. Scrapy对接Selenium Scrapy抓取页面的方式和requests库类似,都是直接模拟HTTP请求,而Scrapy也不能抓取JavaScript动态谊染的页面.在前面的博客中抓取Ja ...

  6. Python 之scrapy框架58同城招聘爬取案例

    一.项目目录结构: 代码如下: # -*- coding: utf-8 -*- # Define here the models for your scraped items # # See docu ...

  7. python爬虫 scrapy框架(一)爬取壁纸照片

    此项目仅供学习参考, 不用于任何商业用途 若侵权留言,立刻删除 刚入门爬虫不久,一心想找个网站试试,然后朋友推荐了这个壁纸网站   

  8. 爬虫系列---scrapy全栈数据爬取框架(Crawlspider)

    一 简介 crawlspider 是Spider的一个子类,除了继承spider的功能特性外,还派生了自己更加强大的功能. LinkExtractors链接提取器,Rule规则解析器. 二 强大的链接 ...

  9. 全栈爬取-Scrapy框架(CrawlSpider)

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

随机推荐

  1. mongodb "Element '{0}' does not match any field or property of class" 异常的解决方法

    在序列化的对象上增加 [BsonIgnoreExtraElements]

  2. eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建

    这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 接下来马上进入项目搭建 ...

  3. vb.net 使用NPO自定義格式

    '導入命名空間 Imports System.IOImports NPOI.HSSF.UserModelImports NPOI.HPSFImports NPOI.POIFS.FileSystem P ...

  4. Fundebug能够捕获这些BUG

    摘要:Fundebug的JavaScript监控插件更新至0.1.0,可以监控3种不同类型的前端BUG:JavaScript执行错误.资源加载错误.HTTP请求错误. 从简单的onerror开始,Fu ...

  5. canvas-8searchLight4.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. angular 拼接html 事件无效

    主要是要引用$compile方法

  7. Android为TV端助力 帧动画

    首先在res/drawable/name1.xml/定义一组图片集合: <?xml version="1.0" encoding="utf-8"?> ...

  8. 深入理解Java虚拟机01--概述

    本课题是对<深入理解Java虚拟机>周志明 第二版的总结   具体可以参考:https://pan.baidu.com/s/1v_mPp--XV4u4rCBMkbR37A 第1版 可以忽略 ...

  9. python模块--collections

    python的内建模块collections有几个关键的数据结构,平常在使用的时候,开发者可以直接调用,不需要自己重复制造轮子,这样可以提高开发效率. 1. deque双端队列 平常我们使用的pyth ...

  10. java数据结构 • 面向对象 • 异常 • 随机数·时间

    • 语法基础 • 控制流 • 数据结构 • 面向对象 • 异常 • 随机数 //String常用的方法: indexOf   charAt   charAt   codePointAt   compa ...