需求:

使用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. pyinstaller 打包

    [root@mhc nsf]# pip install pyinstallerCollecting pyinstaller  Downloading PyInstaller-3.3.tar.gz (3 ...

  2. Creating Cubemaps in Unity3D

    [Creating Cubemaps in Unity3D] 1.在Editor目录下生成GenerateStaticCubemap.cs. 2.编写代码,生成一个继承于ScriptableWizar ...

  3. 【SPOJ - SUBLEX】Lexicographical Substring Search 【后缀自动机+dp】

    题意 给出一个字符串和q个询问,每个询问给出一个整数k,输出第k大得子串. 分析 建后缀自动机,利用匹配边来解决.设d[v]为从状态v开始有多少不同的路径.这个显然是可以递推出来的.然后对于每个询问, ...

  4. 字符串匹配——C++使用Regex

    需要#include  < regex >   匹配 regex_match ("subject", std::regex("(sub)(.*)") ...

  5. 微信OAuth2.0网页授权php示例

    1.配置授权回调页面域名,如 www.aaa.com 2.模拟公众号的第三方网页,fn_system.php <?php if(empty($_SESSION['user'])){ header ...

  6. 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值

    [抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

  7. spring中添加google的guava缓存(demo)

    1.pom文件中配置 <dependencies> <dependency> <groupId>org.springframework</groupId> ...

  8. jQuery中deferred对象的使用(一)

    在jquery1.5之后的版本中,加入了一个deferred对象,也就是延迟对象,用来处理未来某一时间点发生的回调函数.同时,还改写了ajax方法,现在的ajax方法返回的是一个deferred对象. ...

  9. Greeplum 系列(三) 基本用法

    Greeplum 系列(三) 基本用法 <PostgreSQL 教程>:https://www.yiibai.com/postgresql 一.Greeplum 登陆与创建 1.1 登陆 ...

  10. jquery Ajax跨域请求

    这是jquery api文档对跨域请求的解析:如果获取的数据文件存放在远程服务器上(域名不同,也就是跨域获取数据),则需要使用JSONP类型.使用这种类型的话,会创建一个查询字符串参数 callbac ...