利用scrapy框架抓取腾讯的招聘信息,爬取地址为:https://hr.tencent.com/position.php

抓取字段包括:招聘岗位,人数,工作地点,发布时间,及具体的工作要求和工作任务

最终结果保存为两个文件,一个文件放前面的四个字段信息,一个放具体内容信息

1.网页分析

通过网页源码和F12显示的代码对比发现,该网页属于静态网页。

可以采用xpath解析网页源码,获取tr标签下的相关内容,具体见代码部分。

2.编辑items.py文件

通过scrapy startproject + 项目名称 生成项目后,来到items.py文件下,首先定义爬取的字段。

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html import scrapy class TencentItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field() # 职位名称
position_name = scrapy.Field()
# 职位类别
position_type = scrapy.Field()
# 招聘人数
wanted_number = scrapy.Field()
# 工作地点
work_location = scrapy.Field()
# 发布时间
publish_time = scrapy.Field()
# 详情信息
position_link = scrapy.Field() class DetailsItem(scrapy.Item):
"""
将详情页提取到的数据另外保存到一个文件中
"""
# 工作职责
work_duties = scrapy.Field()
# 工作要求
work_skills = scrapy.Field()

3.编写爬虫部分

使用scrapy genspiders + 名称+初始url,生成爬虫后,来到spiders文件夹下的爬虫文件,编写爬虫逻辑,具体代码如下:

# -*- coding: utf-8 -*-
import scrapy # 导入待爬取字段名
from tencent.items import TencentItem, DetailsItem class TencentWantedSpider(scrapy.Spider):
name = 'tencent_wanted'
allowed_domains = ['hr.tencent.com']
start_urls = ['https://hr.tencent.com/position.php'] base_url = 'https://hr.tencent.com/' def parse(self, response): # 获取页面中招聘信息在网页中位置节点
node_list = response.xpath('//tr[@class="even"] | //tr[@class="odd"]') # 匹配到下一页的按钮
next_page = response.xpath('//a[@id="next"]/@href').extract_first() # 遍历节点,进入详情页,获取其他信息
for node in node_list:
# 实例化,填写数据
item = TencentItem() item['position_name'] = node.xpath('./td[1]/a/text()').extract_first()
item['position_link'] = node.xpath('./td[1]/a/@href').extract_first()
item['position_type'] = node.xpath('./td[2]/text()').extract_first()
item['wanted_number'] = node.xpath('./td[3]/text()').extract_first()
item['work_location'] = node.xpath('./td[4]/text()').extract_first()
item['publish_time' ] = node.xpath('./td[5]/text()').extract_first() yield item
yield scrapy.Request(url=self.base_url + item['position_link'], callback=self.details) # 访问下一页信息
yield scrapy.Request(url=self.base_url + next_page, callback=self.parse) def details(self, response):
"""
对详情页信息进行抽取和解析
:return:
"""
item = DetailsItem()
# 从详情页获取工作责任和工作技能两个字段名
item['work_duties'] = ''.join(response.xpath('//ul[@class="squareli"]')[0].xpath('./li/text()').extract())
item['work_skills'] = ''.join(response.xpath('//ul[@class="squareli"]')[1].xpath('./li/text()').extract())
yield item

4.编写pipelines.py文件,对抓取数据进行保存。

对爬取的数据进行保存,首先要在settings.py文件里,注册爬虫的管道信息,如:

具体代码如下:

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html import json
from tencent.items import TencentItem, DetailsItem
class TencentPipeline(object):
def open_spider(self, spider):
"""
爬虫运行时,执行的方法
:param spider:
:return:
"""
self.file = open('tenc_wanted_2.json', 'w', encoding='utf-8')
self.file_detail = open('tenc_wanted_detail.json', 'w', encoding='utf-8') def process_item(self, item, spider): content = json.dumps(dict(item), ensure_ascii=False) # 判断数据来源于哪里(是哪个类的实例),写入对应的文件
if isinstance(item, TencentItem):
self.file.write(content + '\n') if isinstance(item, DetailsItem):
self.file_detail.write(content + '\n') return item def close_spider(self, spider):
"""
爬虫运行结束后执行的方法
:param spider:
:return:
"""
self.file.close()
self.file_detail.close()

5.运行结果

6.完整代码

参见:https://github.com/zInPython/Tencent_wanted

利用scrapy爬取腾讯的招聘信息的更多相关文章

  1. python之scrapy爬取jd和qq招聘信息

    1.settings.py文件 # -*- coding: utf-8 -*- # Scrapy settings for jd project # # For simplicity, this fi ...

  2. scrapy爬取全部知乎用户信息

    # -*- coding: utf-8 -*- # scrapy爬取全部知乎用户信息 # 1:是否遵守robbots_txt协议改为False # 2: 加入爬取所需的headers: user-ag ...

  3. 利用 Scrapy 爬取知乎用户信息

    思路:通过获取知乎某个大V的关注列表和被关注列表,查看该大V和其关注用户和被关注用户的详细信息,然后通过层层递归调用,实现获取关注用户和被关注用户的关注列表和被关注列表,最终实现获取大量用户信息. 一 ...

  4. python3 scrapy 爬取腾讯招聘

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

  5. 利用Crawlspider爬取腾讯招聘数据(全站,深度)

    需求: 使用crawlSpider(全站)进行数据爬取 - 首页: 岗位名称,岗位类别 - 详情页:岗位职责 - 持久化存储 代码: 爬虫文件: from scrapy.linkextractors ...

  6. 利用Scrapy爬取所有知乎用户详细信息并存至MongoDB

    欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者 :崔庆才 本节分享一下爬取知乎用户所有用户信息的 Scrapy 爬虫实战. 本节目标 本节要实现的内容有 ...

  7. <scrapy爬虫>爬取腾讯社招信息

    1.创建scrapy项目 dos窗口输入: scrapy startproject tencent cd tencent 2.编写item.py文件(相当于编写模板,需要爬取的数据在这里定义) # - ...

  8. 利用scrapy爬取文件后并基于管道化的持久化存储

    我们在pycharm上爬取 首先我们可以在本文件打开命令框或在Terminal下创建 scrapy startproject xiaohuaPro   ------------创建文件 scrapy ...

  9. Python爬虫从入门到放弃(十八)之 Scrapy爬取所有知乎用户信息(上)

    爬取的思路 首先我们应该找到一个账号,这个账号被关注的人和关注的人都相对比较多的,就是下图中金字塔顶端的人,然后通过爬取这个账号的信息后,再爬取他关注的人和被关注的人的账号信息,然后爬取被关注人的账号 ...

随机推荐

  1. (七)javac编译

    文章目录 1.基本格式 2.目标路径 2.1 缺省项 2.2 指定路径 2.2.1 全路径 2.2.2 相对路径 3.源文件 3.1 无第三方库 3.1.1 基本方法 3.1.2 添加目录 3.1.3 ...

  2. ios下app内嵌h5页面是video适配问题

    ios下做新闻详情用h5页面实现然后打包到app中,其中新闻详情页会有视频,安卓下video的poster可以做到适应video大小,但是ios下会按照poster图片大小将video等比撑大,但是视 ...

  3. 入门Android底层需要的一些技能

    <Android的设计与实现> Android框架层<Linux系统编程手册> Linux系统编程<Android内核剖析> 编译框架和romC语言和Linux内核 ...

  4. 智和网管平台SugarNMS助力网络安全运维等保2.0建设

    智和信通智和网管平台SugarNMS结合<信息安全技术 网络安全等级保护基本要求>(GB/T 22239-2019)等国家标准文件以及用户提出的网络安全管理需求进行产品设计,推出“监控+展 ...

  5. abp(net core)+easyui+efcore实现仓储管理系统——EasyUI之货物管理八(二十六)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  6. 19.7.29 NOIP模拟10

    话说这次三道考试题直接可以连成一个段子:我一个辣鸡,连模板都不会打,只能跪倒在大佬面前; T1 辣鸡 但是我实在是太辣鸡了,最后干的T1,时间不够用,连暴力都没打对,无奈之下交了一个qj程序,60分( ...

  7. BigInt 的使用!

    今天学长讲的卡特兰数真的是卡的一批,整个全是高精的题,这时我就使用重载运算符,然后一下午就过去了 首先来看一波水题(也就卡了2小时) . A. 网格 内存限制:512 MiB 时间限制:1000 ms ...

  8. VM 使用问题 | 安装失败->>注册表

    下午乌龙了一回,本来就知道注册表都卸载的乱乱的 以为安装上即可,越弄越糊涂 无法安装.... 查了注册表,发现那些都删除了 手动安装实在太过麻烦,弄了一早上. 如图:未能解决 ​ ​ ​ 后使用了清洁 ...

  9. 0911作业-if while循环小练习

    输入姑娘的年龄后,进行以下判断: 如果姑娘小于18岁,打印"不接受未成年" 如果姑娘大于18岁小于25岁,打印"心动表白" 如果姑娘大于25岁小于45岁,打印& ...

  10. python——inspect模块

    inspect模块常用功能 import inspect # 导入inspect模块 inspect.isfunction(fn) # 检测fn是不是函数 inspect.isgenerator((x ...