CrawlSpiders
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的更多相关文章
- CrawlSpiders模块的使用
创建文件模板 scrapy genspider -t crawl tencent tencent.com CrawlSpiders就是为爬取整站孕育而生的,我们以前是分页下一页,然后再yied.这样太 ...
- 11.CrawlSpiders
CrawlSpiders 通过下面的命令可以快速创建 CrawlSpider模板 的代码: .scrapy startproject tencentspider .scrapy genspider - ...
- 爬虫框架Scrapy之CrawlSpiders
CrawlSpiders 通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl tencent tencent.com 上一个案例中,我 ...
- scrapy之CrawlSpiders
CrawlSpiders 通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl loaderan cnblogs.com class s ...
- CrawlSpiders简介
转:https://www.cnblogs.com/ellisonzhang/p/11124516.html#4295547 一.CrawlSpiders类简介 通过下面的命令可以快速创建 Crawl ...
- scrapy基础知识之 CrawlSpiders爬取lagou招聘保存在mysql(分布式):
items.py import scrapy class LagouItem(scrapy.Item): # define the fields for your item here like: # ...
- scrapy基础知识之 CrawlSpiders(爬取腾讯校内招聘):
import scrapyfrom scrapy.spider import CrawlSpider,Rulefrom scrapy.linkextractors import LinkExtract ...
- scrapy基础知识之 CrawlSpiders:
通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl spidername xx.com LinkExtractors class sc ...
- 三、scrapy后续
CrawlSpiders 通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl tencent tencent.com 我们通过正则表达 ...
随机推荐
- Centos下配置tomcat7的https证书
近期搞定了HTTPS配置,特此记录. 1.把下载的文件拷贝到cert文件夹,然后放在tomcat根目录下(与conf同一级目录).2.配置conf下的server.xml,修改下面3个节点,如下: & ...
- Python 标准库 urllib2 的使用细节(转)
http://www.cnblogs.com/yuxc/archive/2011/08/01/2123995.html http://blog.csdn.net/wklken/article/deta ...
- Hi3518EV200平台ADC多通道采样
Hi3518EV200平台ADC多通道采样流程 Hi3518EV200 ADC 本文针对Hi3518EV200平台处理器,通过ADC单次采样方式,实现对多通道(1~4通道)ADC进行采样控制.本文仅仅 ...
- Orchard Core一分钟搭建ASP.NET Core CMS
Orchard Core 是Orchard CMS的ASP.NET Core版本. Orchard Core是全新一代的ASP.NET Core CMS. 官方文档介绍:http://orchardc ...
- 机器学习(二)-kNN手写数字识别
一.kNN算法是机器学习的入门算法,其中不涉及训练,主要思想是计算待测点和参照点的距离,选取距离较近的参照点的类别作为待测点的的类别. 1,距离可以是欧式距离,夹角余弦距离等等. 2,k值不能选择太大 ...
- SQL---存储过程---存储过程编写案例
存储过程的创建和调用演示 1.不带参数的存储过程的创建 create procedure PRO_With_No_Param as Begin --begin可省略 select * from sc ...
- WebApi接收复杂类型参数
当接收实体时,该实体类不能添加Serializable属性,否则传来的json数据无法映射成功?
- NTP时间同步 服务端 客户端 自动化安装配置
NTP时间同步 服务端 客户端 自动化安装配置 原创内容 http://www.cnblogs.com/elvi/p/7657994.html #!/bin/sh #运行环境 centos6.cent ...
- P1040 加分二叉树
转自:(http://www.cnblogs.com/geek-007/p/7197439.html) 经典例题:加分二叉树(Luogu 1040) 设一个 n 个节点的二叉树 tree 的中序遍历为 ...
- 企业级分布式监控系统-Zabbix基础
1.基础分部 1.1Zabbix简介 Zabbix 是一个企业级的分布式开源监控方案. 1.2监控系统架构 C/S架构 客户端/服务器端,这种架构适合规模较小,处于同一地域的环境 C/P/S 客户端/ ...