1.用 scrapy 新建一个 tencent 项目

2.在 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 TencentItem(scrapy.Item):
# define the fields for your item here like:
# 职位
position_name = scrapy.Field()
# 详情链接
positin_link = scrapy.Field()
# 职业类别
position_type = scrapy.Field()
# 招聘人数
people_number = scrapy.Field()
# 工作地点
work_location = scrapy.Field()
# 发布时间
publish_time = scrapy.Field()

3.快速创建 CrawlSpider模板

scrapy genspider -t crawl tencent_spider tencent.com

注意  此时中的名称不能与项目名相同

4.打开tencent_spider.py 编写代码

 # -*- coding: utf-8 -*-
import scrapy
# 导入链接规则匹配类,用来提取符合规则的链接
from scrapy.linkextractors import LinkExtractor
# 导入CrawlSpider类和Rule
from scrapy.spiders import CrawlSpider, Rule
# 从tentcent项目下的itmes.py中导入TencentItem类
from tencent.items import TencentItem class TencentSpiderSpider(CrawlSpider):
name = 'tencent_spider'
allowed_domains = ['hr.tencent.com']
start_urls = ['http://hr.tencent.com/position.php?&start=0#a']
pagelink = LinkExtractor(allow=("start=\d+")) # 正则匹配 rules = (
# 获取这个列表的链接,依次发送请求,并继续跟进,调用指定的回调函数
Rule(pagelink, callback='parse_item', follow=True),
) def parse_item(self, response):
for each in response.xpath("//tr[@class='even'] | //tr[@class='odd']"):
item = TencentItem()
# 职位名称
item['position_name'] = each.xpath("./td[1]/a/text()").extract()[0]
# 详情连接
item['position_link'] = each.xpath("./td[1]/a/@href").extract()[0]
# 职位类别
#item['position_type'] = each.xpath("./td[2]/text()").extract()[0]
# 招聘人数
item['people_number'] = each.xpath("./td[3]/text()").extract()[0]
# 工作地点
# item['work_location'] = each.xpath("./td[4]/text()").extract()[0]
# 发布时间
item['publish_time'] = each.xpath("./td[5]/text()").extract()[0] yield item

5.在 piplines.py 中写入文件

 1 # -*- coding: utf-8 -*-
2
3 # Define your item pipelines here
4 #
5 # Don't forget to add your pipeline to the ITEM_PIPELINES setting
6 # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
7
8 import json
9
10 class TencentPipeline(object):
11 def open_spider(self, spider):
12 self.filename = open("tencent.json", "w")
13
14 def process_item(self, item, spider):
15 text = json.dumps(dict(item), ensure_ascii = False) + "\n"
16 self.filename.write(text.encode("utf-8")
17 return item
18
19 def close_spider(self, spider):
20 self.filename.close()

7.在命令输入以下命令运行

scrapy crawl tencen_spider.py

出现以下问题在tencent_spider.py 文件中只有把position_type 和 work_location 注销掉才能运行...

CrawlSpiders的更多相关文章

  1. CrawlSpiders模块的使用

    创建文件模板 scrapy genspider -t crawl tencent tencent.com CrawlSpiders就是为爬取整站孕育而生的,我们以前是分页下一页,然后再yied.这样太 ...

  2. 11.CrawlSpiders

    CrawlSpiders 通过下面的命令可以快速创建 CrawlSpider模板 的代码: .scrapy startproject tencentspider .scrapy genspider - ...

  3. 爬虫框架Scrapy之CrawlSpiders

    CrawlSpiders 通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl tencent tencent.com 上一个案例中,我 ...

  4. scrapy之CrawlSpiders

    CrawlSpiders 通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl loaderan cnblogs.com class s ...

  5. CrawlSpiders简介

    转:https://www.cnblogs.com/ellisonzhang/p/11124516.html#4295547 一.CrawlSpiders类简介 通过下面的命令可以快速创建 Crawl ...

  6. scrapy基础知识之 CrawlSpiders爬取lagou招聘保存在mysql(分布式):

    items.py import scrapy class LagouItem(scrapy.Item): # define the fields for your item here like: # ...

  7. scrapy基础知识之 CrawlSpiders(爬取腾讯校内招聘):

    import scrapyfrom scrapy.spider import CrawlSpider,Rulefrom scrapy.linkextractors import LinkExtract ...

  8. scrapy基础知识之 CrawlSpiders:

    通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl spidername xx.com LinkExtractors class sc ...

  9. 三、scrapy后续

    CrawlSpiders 通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl tencent tencent.com 我们通过正则表达 ...

随机推荐

  1. API设计相关

    来自HeroKu的HTTP API 设计指南 http://get.jobdeer.com/343.get https://github.com/interagent/http-api-design ...

  2. RabbitMQ之比较好的资料

    http://mysql.taobao.org/index.php/Rabbitmq http://www.cnblogs.com/me-sa/archive/2012/10/17/rabbitmq_ ...

  3. 深入理解立即执行函数(function(){})();

    ( function(){-} )()和( function (){-} () )是两种javascript立即执行函数的常见写法,要理解立即执行函数,需要先理解一些函数的基本概念. 1,函数声明,函 ...

  4. iOS学习——如何在mac上获取开发使用的模拟器的资源以及模拟器中每个应用的应用沙盒

    如题,本文主要研究如何在mac上获取开发使用的模拟器的资源以及模拟器中每个应用的应用沙盒.做过安卓开发的小伙伴肯定很方便就能像打开资源管理器一样查看我们写到手机本地或应用中的各种资源,但是在iOS开发 ...

  5. es6环境搭建

    安装node环境 地址:https://nodejs.org/en/download/ 建立项目目录 建立一个项目目录es6-demo,并在目录下建立两个子文件夹src和dist: src:源代码es ...

  6. 一个在java后台实现的对图片进行加网纹或水印的工具类

    import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  7. android TransFormexecption 解决

    近期编译的时候遇到这个问题.... > com.android.build.api.transform.Transformexception: java.util.zip.ZipExceptio ...

  8. mysql--二进制日志(bin-log)三种格式介绍及分析

    一.Mysql binlog日志有三种格式,分别为ROW.Statement以及MiXED.Row LevelBinary Log会记录成[每一行数据被修改的形式],然后在Slave端再对相同的数据进 ...

  9. 地图开发GIS的应用有哪些?

    GIS的应用领域有哪些? 地理信息系统在最近的30多年内取得了惊人的发展,广泛应用于资源调查.环境评估.灾害预测.国土管理.城市规划.邮电通讯.交通运输.军事公安.水利电力.公共设施管理.农林牧业.统 ...

  10. java变量和作用域以及成员变量的默认初始化

    Java中的变量有成员变量和局部变量,定义在类中方法之外的变量成为成员变量或者成员字段(域),表示一个类所具有的属性,定义为类的成员变量的变量的作用于是整个类,该变量在定义的时候不需要初始化,在使用前 ...