栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

腾讯职位招聘案例

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

腾讯职位招聘案例

1. ⽬标⽹站:腾讯招聘 2. 需求:
  • 爬取招聘岗位信息(岗位名称 + ⼯作要求)
  • 翻⻚
3.分析⽹站:

动态加载数据

# 起始URL
start_urls = [
    'https://careers.tencent.com/'
    'tencentcareer/api/post/Query?timestamp=1656484885985&countryId=&cityId=&'
    'bgIds=&productId=&categoryId=40001001,40001002,40001003,40001004,40001005,40001006&parentCategoryId=&'
    'attrId=&keyword=&pageIndex=2&pageSize=10&language=zh-cn&area=cn']

# 详情页URL
duty_url = 'https://careers.tencent.com/tencentcareer/api/post/ByPostId?' 
           'timestamp=1656484967344&postId={}&language=zh-cn'

4.爬取思路:
  1. 返回url对应的response响应对应的数据
  2. 解析第⼀⻚上每个岗位PostId和岗位名称
  3. 构造每个岗位详情⻚url
  4. 解析详情⻚url,提取⼯作要求
  5. 在管道用csv保存数据,并在settings开启管道
5.源代码 T_spider.py
import scrapy
import json
from fake_useragent import UserAgent
from Tencent2.items import Tencent2Item

class TSpiderSpider(scrapy.Spider):
    name = 'T_spider'

    # allowed_domains = ['careers.tencent.com/search.html?pcid=40001']

    # 设置请求头
    headers = {
        'user-agent':UserAgent().random
    }

    # 起始URL
    start_urls = [
        'https://careers.tencent.com/'
        'tencentcareer/api/post/Query?timestamp=1656484885985&countryId=&cityId=&'
        'bgIds=&productId=&categoryId=40001001,40001002,40001003,40001004,40001005,40001006&parentCategoryId=&'
        'attrId=&keyword=&pageIndex=2&pageSize=10&language=zh-cn&area=cn']

    # 详情页URL
    duty_url = 'https://careers.tencent.com/tencentcareer/api/post/ByPostId?' 
               'timestamp=1656484967344&postId={}&language=zh-cn'

    def start_requests(self):
        for page in range(1, 10):
            url = self.start_urls[0].format(page)

            # 翻页发送请求
            yield scrapy.Request(url=url, callback=self.parse,headers=self.headers)

    def parse(self, response):
        # 将josn数据转换为python字典数据
        data = json.loads(response.text)['Data']['Posts']

        for i in data:
            # 实例化对象
            item = Tencent2Item()
            item['job_name'] = i['RecruitPostName']

            PostId =  i['PostId']

            # 构造请求
            yield scrapy.Request(url=self.duty_url.format(PostId),callback=self.detail_parse,meta={'item':item})


    def detail_parse(self,response):
        """详情页的爬取"""
        
        # 接受上个函数的参数
        item = response.meta.get('item')

        # 将josn数据转换为python字典数据
        data = json.loads(response.text)['Data']
        item['job_duty'] = data['Responsibility']

        yield item
items.py
import scrapy


class Tencent2Item(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    job_name = scrapy.Field()
    job_duty = scrapy.Field()
pipelines.py
import csv


class Tencent2Pipeline:
    def __init__(self):
        self.f = open('tencent.csv', encoding='utf-8-sig', newline='', mode='w')

    def open_spider(self, spider):
        self.w = csv.DictWriter(self.f, fieldnames=['job_name', 'job_duty'])
        self.w.writeheader()

    def close_spider(self, spider):
        self.f.close()

    def process_item(self, item, spider):
        self.w.writerow(dict(item))

        return item
settings.py
ITEM_PIPELINES = {
   'Tencent2.pipelines.Tencent2Pipeline': 300,
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/997540.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号