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. IDEA设置显示中文文档API方法说明

    首先,我们从网上下载好对应的java最新的中文api文档,chm格式的 chm其实相当于一个压缩包,里面有许多html文件 让IDEA显示中文文档,其实原理就是是让IDEA把java的api的对应ht ...

  2. 元类实现ORM

    1. ORM是什么 ORM 是 python编程语言后端web框架 Django的核心思想,"Object Relational Mapping",即对象-关系映射,简称ORM. ...

  3. SpringBoot的Autowierd失败

    通常是以下几种可能: 1.没有加@Service注解,或者是这个bean没有放在标注了@Configuration这个注解的类下. 2.SpringBoot启动类没有开启扫描 @ComponentSc ...

  4. oracle的Date类型遇到MyBatis产生的坑

    坑描述: 公司的订单表数据量巨大(亿级),在进行查询的时候,发现一个慢查询. 背景: 数据库:oracle 表:T_order 索引字段:create_date  (字段类型 date) 慢查询sql ...

  5. SaaS技术栈的走势

    本地部署时代 在软件还是“本地部署(on-premise)”的时候,SaaS的版图被大型玩家把持着,几乎所有的垂直领域(营销.支持.销售.人力)都被微软.SAP等大公司的解决方案占据.那时候的用户并没 ...

  6. 发生服务器错误: Error loading MySQLdb module: libmysqlclient.so.18: cannot open shared object file: No such file or directory

    在hue上配置Mysql的时候,出现的错误:  发生服务器错误: Error loading MySQLdb module: libmysqlclient.so.18: cannot open sha ...

  7. JS之onunload、onbeforeunload事件详解

    简介 onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过 window.onunload来调用.区别在于onbeforeunload在o ...

  8. Spring学习之旅(三)Spring工作原理初探

    详细的废话相信很多书籍视频资料都已经很多了,这里说几个小编个人认为对于理解Spring框架很重要的点.欢迎批评指正. 1)Spring的控制反转 先说说“依赖”,在面向对象程序设计中,类A中用到了类B ...

  9. ASP.NET MVC Json的序列化和反序列化

    1.利用js进行序列化成字符串和反序列化 var personObj = {name:"Tom",age:16}; // 利用JS序列化成字符串 var personStr = J ...

  10. Testlink1.9.17使用方法(第九章 测试结果分析)

    第九章 测试结果分析 QQ交流群:585499566 TestLink根据测试过程中记录的数据,提供了较为丰富的度量统计功能,可以直观的得到测试管理过程中需要进行分析和总结的数据.点击首页横向导航栏中 ...