需求:

使用crawlSpider(全站)进行数据爬取

- 首页: 岗位名称,岗位类别

- 详情页:岗位职责

- 持久化存储

代码:

爬虫文件:

from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from ..items import CrawlproItem,TenproItem_detail class CrawSpider(CrawlSpider):
name = 'craw'
# allowed_domains = ['www.xxx.com']
start_urls = ['https://hr.tencent.com/position.php?&start=0#a']
# 首页所有页码的连接提取器
link1 = LinkExtractor(allow=r'&start=\d+#a')
# 详情页连接提取器
link2 = LinkExtractor(allow=r'position_detail.php\?id=\d+&keywords=&tid=0&lid=0$') # 问号转义 rules = (
Rule(link1, callback='parse_item', follow=True),
Rule(link2, callback='parse_detail', follow=True),
) def parse_item(self, response):
# 岗位名称和类别
print('item',response)
tr_list = response.xpath('//table[@class="tablelist"]/tr[@class="odd"] | //table[@class="tablelist"]/tr[@class="even"]')
for tr in tr_list:
job_name = tr.xpath('./td[1]/a/text()').extract_first()
job_class = tr.xpath('./td[2]/text()').extract_first()
# 实例化item类
item = CrawlproItem()
item['job_name'] = job_name
item['job_class'] = job_class yield item def parse_detail(self, response):
# 岗位职责
desc = response.xpath('//ul[@class="squareli"]/li/text()').extract()
desc = ''.join(desc)
item = TenproItem_detail()
item['desc'] = desc
yield item

items.py文件:

import scrapy

class CrawlproItem(scrapy.Item):
job_name = scrapy.Field()
job_class = scrapy.Field() class TenproItem_detail(scrapy.Item):
desc = scrapy.Field()

管道文件pipelines.py:

class CrawlproPipeline(object):
fp = None def open_spider(self, spider):
# 文件只打开一次
self.fp = open('./tenxun.txt', 'w',encoding='utf-8') def process_item(self, item, spider):
desc = None
# 取出item中的值 if item.__class__.__name__ == 'CrawlproItem':
job_name = item["job_name"]
job_class = item["job_class"]
self.fp.write(f'{job_name}\n{job_class}\n\n')
else:
desc = item['desc']
self.fp.write(desc)
return item # 返回给下一个即将被执行的管道类 def close_spider(self, spider):
self.fp.close()

配置文件中注意开启管道

利用Crawlspider爬取腾讯招聘数据(全站,深度)的更多相关文章

  1. 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息

    简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 系统环境:Fedora22(昨天已安装scrapy环境) 爬取的开始URL:ht ...

  2. 利用python爬取58同城简历数据

    利用python爬取58同城简历数据 利用python爬取58同城简历数据 最近接到一个工作,需要获取58同城上面的简历信息(http://gz.58.com/qzyewu/).最开始想到是用pyth ...

  3. 利用scrapy爬取腾讯的招聘信息

    利用scrapy框架抓取腾讯的招聘信息,爬取地址为:https://hr.tencent.com/position.php 抓取字段包括:招聘岗位,人数,工作地点,发布时间,及具体的工作要求和工作任务 ...

  4. Python 爬取腾讯招聘职位详情 2019/12/4有效

    我爬取的是Python相关职位,先po上代码,(PS:本人小白,这是跟着B站教学视频学习后,老师留的作业,因为腾讯招聘的网站变动比较大,老师的代码已经无法运行,所以po上),一些想法和过程在后面. f ...

  5. 『Scrapy』爬取腾讯招聘网站

    分析爬取对象 初始网址, http://hr.tencent.com/position.php?@start=0&start=0#a (可选)由于含有多页数据,我们可以查看一下这些网址有什么相 ...

  6. python3 scrapy 爬取腾讯招聘

    安装scrapy不再赘述, 在控制台中输入scrapy startproject tencent 创建爬虫项目名字为 tencent 接着cd tencent 用pycharm打开tencent项目 ...

  7. Python爬虫入门——使用requests爬取python岗位招聘数据

    爬虫目的 使用requests库和BeautifulSoup4库来爬取拉勾网Python相关岗位数据 爬虫工具 使用Requests库发送http请求,然后用BeautifulSoup库解析HTML文 ...

  8. 利用Jsoup爬取新冠疫情数据并存至数据库

    需要用到的jar包(用来爬取的jsoup,htmlunit-2.37.0-bin以及连接数据库中的mysql.jar) 链接:https://pan.baidu.com/s/1VlylWmlhjd8K ...

  9. scrapy 第一个案例(爬取腾讯招聘职位信息)

    import scrapy import json class TzcSpider(scrapy.Spider): # spider的名字,唯一 name = 'tzc' # 起始地址 start_u ...

随机推荐

  1. Vertex Lit

    [Vertex Lit] Vertex Lit path generally renders each object in one pass, with lighting from all light ...

  2. __str__&__repr__

    [__str__&__repr__] object.__str__(self): Called by the str() built-in function and by the print  ...

  3. Kafka集群中 topic数据的分区 迁移到其他broker

    前言 kafka集群扩容后,新的broker上面不会数据进入这些节点,也就是说,这些节点是空闲的:它只有在创建新的topic时才会参与工作.除非将已有的partition迁移到新的服务器上面:所以需要 ...

  4. .net中delegate的使用

    js中的写法: var GetEntityList = function(pParameter){ var list =[]; return list; }; var aEntityList = Ge ...

  5. linux shell脚本编程笔记(二): 分支结构

    1.if if command then commands fi if command then commands else commands fi if command1 then command ...

  6. Unity中Avatar换装实现

    http://www.cnblogs.com/herenzhiming/articles/6533162.html

  7. WebAPI的路由规则

    1.自定义路由 public static class WebApiConfig { public static void Register(HttpConfiguration config) { / ...

  8. loadrunner怎么进行内容检查

    运行测试时,常常需要验证某些内容是否出现在返回的页面上.内容检查验证脚本运行时 Web 页面上是否出现期望的信息.可以插入两种类型的内容检查:➤ 文本检查.检查文本字符串是否出现在 Web 页面上.➤ ...

  9. Python代码规范利器Flake8

    写代码其实是需要规范的,团队中更是如此:不然 Google 也不会发布各种编码规范,耳熟能详的有Google C++ 风格指南,Google Python 风格指南,等等. 这些规范有用吗?有用也没用 ...

  10. 白盒测试实践项目(day3)

    李建文同学的白盒缺陷报告已经提交,正在由组长胡俊辉同学进行审阅,查看并发现是否有什么不足,再由小组讨论补充. 汪鸿同学的静态代码工具熟悉已经初步完成,并且准备撰写文档. 杨瑞丰同学的Mock测试方法也 ...