腾讯招聘信息 爬取案例

前段时间看了了scrapy,有一个案例是爬取腾讯招聘信息的,当时看了腾讯网站,已经更新换代了,于是自己看了看,写下一个实际可行的demo

首先是观察腾讯招聘首页不在页面element内,但是依旧很容易就可以发现你要的信息

在这里插入图片描述
观察url,可以提取出 https://careers.tencent.com/tencentcareer/api/post/Query?pageIndex=1&pageSize=10
pageIndex是页码,pageSize是当页容量

接下来练练手scrapy框架

import scrapy
import json


class HrSpider(scrapy.Spider):
    name = 'hr'
    allowed_domains = ['tencent.com']
    start_urls = ['https://careers.tencent.com/tencentcareer/api/post/Query?pageIndex=1&pageSize=10']

    def __init__(self):
        self.url = 'https://careers.tencent.com/tencentcareer/api/post/Query?pageIndex={}&pageSize=10'
        self.count = 1

    def parse(self, response):
        response = json.loads(response.text)		# response.text可以获取response的文本字符串形式
        li_list = response["Data"]["Posts"]         # 获取当前页数据列表
        count = response["Data"]["Count"]           # 获取数据总计数
        for li in li_list:
            item = {}
            item["title"] = li["RecruitPostName"]
            item["position"] = li["CountryName"] + li["LocationName"]
            item["cate"] = li["CategoryName"]
            item["public_info"] = li["Responsibility"]
            yield item
        # 构造下一页地址
        if self.count*10 <= count:
            self.count += 1
            next_url = self.url.format(self.count)
            yield scrapy.Request(next_url, callback=self.parse)

然后再在pipelines.py添加数据处理,写进mongodb

from itemadapter import ItemAdapter
from pymongo import MongoClient

# 创建数据库实例对象
client = MongoClient(host="192.168.152.145", port=27017)
collection = client["tencent"]["hr"]


class MyspiderPipeline:
    def process_item(self, item, spider):
        collection.insert(item)
        print(item)
        return item

最后就可以在数据库查看我们存储的信息了,很简单,有没有。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值