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. 一:Tomcat 服务器 在45秒内未启动成功

    myeclipse或者eclipse中 tomcat 启动超时怎么办?     修改文件     找到Eclipse的工作空间\.metadata\.plugins\org.eclipse.wst.s ...

  2. [ASP.NET Core 2.0 前方速报]Core 2.0.3 已经支持引用第三方程序集了

    发现问题 在将 FineUIMvc(支持ASP.NET MVC 5.2.3)升级到 ASP.NET Core 2.0 的过程中,我们发现一个奇怪的现象: 通过项目引用 FineUICore 工程一切正 ...

  3. 整合springboot(app后台框架搭建四)

    springboot可以说是为了适用SOA服务出现,一方面,极大的简便了配置,加速了开发速度:第二方面,也是一个嵌入式的web服务,通过jar包运行就是一个web服务: 还有提供了很多metric,i ...

  4. PHP常用的函数与小技巧

    密码加密与验证 password_hash - 创建密码的哈希(hash) string password_hash ( string $password , integer $algo [, arr ...

  5. window下安装Apache+PHP

    本地系统为windows 10,Apache选择httpd-2.4.25-x64-vc14-r1,PHP选择php7.1_x64线程安全版. 1.安装Apache 将apache解压到c:/serve ...

  6. NetFlow学习笔记

    NetFlow学习笔记 标签: netflow 由于工作需要,对NetFlow做了一些学习和调研,并总结成文档以供学习分享. 背景:随着系统的升级与漏洞的修补,入侵主机进而进行破坏的病毒攻击方式在攻击 ...

  7. 初学者没有搞明白的GOROOT,GOPATH,GOBIN,project目录

    我们接下来一个一个来看关于Go语言中的三个目录的详细解释先通过go env查看go的环境变量(我这里是mac的环境,所以可能和你的不同) localhost:~ zhaofan$ go env GOA ...

  8. Python随笔------初探

    今年的双十一刚刚才过去,大多数人主要就是抢购商品,可能他们现在已经收到了他们夜以继日抢购的商品.然而对于我们做技术的,特别是做互联网技术的,我相信肯定都被双十一那天的许多技术震撼到了吧.云计算.分压式 ...

  9. Python带参数的装饰器

    在装饰器函数里传入参数 # -*- coding: utf-8 -*- # 2017/12/2 21:38 # 这不是什么黑魔法,你只需要让包装器传递参数: def a_decorator_passi ...

  10. django事务处理

    #导包 from django.db import transaction try: #django默认是自动提交到数据库,此处设置不让其自动提交 transaction.set_autocommit ...