0
点赞
收藏
分享

微信扫一扫

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘

young_d807 2022-01-31 阅读 60

  大家好,我是不温卜火,是一名计算机学院大数据专业大三的学生,昵称来源于成语—不温不火​,本意是希望自己性情温和​。作为一名互联网行业的小白,博主写博客一方面是为了记录自己的学习过程,另一方面是总结自己所犯的错误希望能够帮助到很多和自己一样处于起步阶段的萌新。但由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教!暂时只在csdn这一个平台进行更新爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_获取数据

PS:由于现在越来越多的人未经本人同意直接爬取博主本人文章,博主在此特别声明:未经本人允许,禁止转载!!!

目录

  • 推荐
  • 一、网页分析
  • 二、功能实现
  • 2.1 拼接URL
  • 2.2 获取数据
  • 三、完整代码
  • 四、保存结果

推荐

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_html_03

  ♥各位如果想要交流的话,可以加下QQ交流群:974178910,里面有各种你想要的学习资料。♥

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_获取数据_04

刚刚经过了豆瓣电影的爬取,你是不是有点懵逼呢?那么博主今天带来一篇较为简单得动态html数据采集的文章。

今天我们来爬取腾讯招聘的相关信息。

链接:https://careers.tencent.com/search.html

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_爬虫_05

一、网页分析

首先我们打开链接,如下图:

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_json_06

通过查看源码,我们发现其并不是静态网页,因此可以初步判定其为动态网页

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_获取数据_07

这样我们的方向就明朗起来了。我们只需找到API接口就可以获取数据。打开开发者选项,通过查找找到我们的API接口

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_html_08

到这里分析已经完成了,那么接下来先尝试获取整个接口信息。因为我们只有先获取数据才有可能继续下一步操作。

# encoding: utf-8
'''
@author 李华鑫
@create 2020-10-19 12:28
Mycsdn:https://buwenbuhuo.blog.csdn.net/
@contact: 459804692@qq.com
@software: Pycharm
@file: test.py
@Version:1.0

'''
import requests

url = "https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1603079775850&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&pageIndex=1&pageSize=10&language=zh-cn&area=cn"

headers = {
"user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
}

def parse_json(url, params={}):
"""解析url,得到字典"""
response = requests.get(url=url, headers=headers, params=params)
return response.json()

content = parse_json(url)
print(content)

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_json_10

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_json_11

????,看来我们是成功获取到数据了。

下面我们来分析下,每一页URL之间的关系。

首先,我们看下其中几个URL

https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1603079775850&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&pageIndex=1&pageSize=10&language=zh-cn&area=cn

https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1603079981956&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&pageIndex=2&pageSize=10&language=zh-cn&area=cn

https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1603079981956&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&pageIndex=3&pageSize=10&language=zh-cn&area=cn

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_python_12

通过对比,我们发现只有pageIndex有变化,那么我们现在可以验证下猜想

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_python_13

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_html_14

通过对比,验证了我们的猜想。下面我们就只需要看总共有多少页即可

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_python_15

分析了页数,那么如果想要循环爬取全部网页,只需进行拼接即可

for i in range(1,635):
params["pageIndex"] = i

二、功能实现

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_python_16

2.1 拼接URL

通过上述的分析,我们知道只需修改每回的pageIndex即可。

首先我们先把需要拼接的部分复制出来

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_json_17

让其转换成字典的方式

xx="""timestamp: 1603081773061
countryId:
cityId:
bgIds:
productId:
categoryId:
parentCategoryId:
attrId:
keyword:
pageIndex: 2
pageSize: 10
language: zh-cn
area: cn"""
xx = xx.splitlines()
params = {}
for x in xx:
print(x.split(":"))
params[x.split(":")[0]] = x.split(":")[1]

from pprint import pprint
pprint(params)

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_获取数据_18

下面就开始代码实现此部分

params = {'area': ' cn',
'attrId': ' ',
'bgIds': ' ',
'categoryId': ' ',
'cityId': ' ',
'countryId': ' ',
'keyword': ' ',
'language': ' zh-cn',
'pageIndex': ' 1',
'pageSize': ' 10',
'parentCategoryId': ' ',
'productId': ' ',
'timestamp': ' 1602211262824'}

def parse_json(url, params={}):
"""解析url,得到字典"""
response = requests.get(url=url, headers=headers, params=params)
return response.json()

def start():
for i in range(1,635):
params["pageIndex"] = i

if __name__ == '__main__':
start()

2.2 获取数据

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_json_19

由于之前已经讲解过此部分,因此此处只给出代码

def get_position(data):
"""获取职位数据"""
item = {
"postion_name":"",#职位名称
"postion_department":"",#职位部门
"postion_location":"",#职位所在地
"postion_country":"",#职位所在国家
"postion_category":"",#职位类别
"postion_responsibility":"",#职位职责
"postion_url":"",#职位url
}
data_list = data["Data"]["Posts"]
for data in data_list:
item["postion_name"] = data["RecruitPostName"]
item["postion_department"] = data["BGName"]
item["postion_location"] = data["LocationName"]
item["postion_country"] = data["CountryName"]
item["postion_category"] = data["CategoryName"]
item["postion_responsibility"] = data["Responsibility"]
item["postion_url"] = data["PostURL"]

三、完整代码

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_爬虫_20

# encoding: utf-8
'''
@author 李华鑫
@create 2020-10-09 9:38
Mycsdn:https://buwenbuhuo.blog.csdn.net/
@contact: 459804692@qq.com
@software: Pycharm
@file: 腾讯招聘.py
@Version:1.0

'''
import requests
import csv

url = "https://careers.tencent.com/tencentcareer/api/post/Query"

headers = {
"user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
}

params = {'area': ' cn',
'attrId': ' ',
'bgIds': ' ',
'categoryId': ' ',
'cityId': ' ',
'countryId': ' ',
'keyword': ' ',
'language': ' zh-cn',
'pageIndex': ' 1',
'pageSize': ' 10',
'parentCategoryId': ' ',
'productId': ' ',
'timestamp': ' 1602211262824'}


def parse_json(url, params={}):
"""解析url,得到字典"""
response = requests.get(url=url, headers=headers, params=params)
return response.json()


def get_position(data):
"""获取职位数据"""
item = {
"postion_name":"",#职位名称
"postion_department":"",#职位部门
"postion_location":"",#职位所在地
"postion_country":"",#职位所在国家
"postion_category":"",#职位类别
"postion_responsibility":"",#职位职责
"postion_url":"",#职位url
}
data_list = data["Data"]["Posts"]
for data in data_list:
item["postion_name"] = data["RecruitPostName"]
item["postion_department"] = data["BGName"]
item["postion_location"] = data["LocationName"]
item["postion_country"] = data["CountryName"]
item["postion_category"] = data["CategoryName"]
item["postion_responsibility"] = data["Responsibility"]
item["postion_url"] = data["PostURL"]

save(item)
print(item)
print("保存完成")

def save(item):
"""将数据保存到csv中"""
with open("./腾讯招聘.csv", "a", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow(item.values())

def start():
for i in range(1,635):
params["pageIndex"] = i
data = parse_json(url,params)
get_position(data)

if __name__ == '__main__':
start()

四、保存结果

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_html_21

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_html_22

美好的日子总是短暂的,虽然还想继续与大家畅谈,但是本篇博文到此已经结束了,如果还嫌不够过瘾,不用担心,我们下篇见!

  好书不厌读百回,熟读课思子自知。而我想要成为全场最靓的仔,就必须坚持通过学习来获取更多知识,用知识改变命运,用博客见证成长,用行动证明我在努力。   如果我的博客对你有帮助、如果你喜欢我的博客内容,请​“点赞” “评论”“收藏”​一键三连哦!听说点赞的人运气不会太差,每一天都会元气满满呦!如果实在要白嫖的话,那祝你开心每一天,欢迎常来我博客看看。   码字不易,大家的支持就是我坚持下去的动力。点赞后不要忘了​关注我哦!

爬虫入门经典(十三) | 一文教你简单爬取腾讯招聘_json_24

举报

相关推荐

0 条评论