大家好,我是不温卜火,是一名计算机学院大数据专业大三的学生,昵称来源于成语—
不温不火,本意是希望自己性情温和。作为一名互联网行业的小白,博主写博客一方面是为了记录自己的学习过程,另一方面是总结自己所犯的错误希望能够帮助到很多和自己一样处于起步阶段的萌新。但由于水平有限,博客中难免会有一些错误出现,有纰漏之处恳请各位大佬不吝赐教!暂时只在csdn这一个平台进行更新,博客主页:https://buwenbuhuo.blog.csdn.net/。
PS:由于现在越来越多的人未经本人同意直接爬取博主本人文章,博主在此特别声明:未经本人允许,禁止转载!!!
目录

推荐

♥各位如果想要交流的话,可以加下QQ交流群:974178910,里面有各种你想要的学习资料。♥
♥欢迎大家关注公众号【不温卜火】,关注公众号即可以提前阅读又可以获取各种干货哦,同时公众号每满1024及1024倍数则会抽奖赠送机械键盘一份+IT书籍1份哟~♥

刚刚经过了豆瓣电影的爬取,你是不是有点懵逼呢?那么博主今天带来一篇较为简单得动态html数据采集的文章。
今天我们来爬取腾讯招聘的相关信息。
链接:https://careers.tencent.com/search.html

一、网页分析
首先我们打开链接,如下图:

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

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


到这里分析已经完成了,那么接下来先尝试获取整个接口信息。因为我们只有先获取数据才有可能继续下一步操作。
1# encoding: utf-8 2''' 3 @author 李华鑫 4 @create 2020-10-19 12:28 5 Mycsdn:https://buwenbuhuo.blog.csdn.net/ 6 @contact: 459804692@qq.com 7 @software: Pycharm 8 @file: test.py 9 @Version:1.0 10 11''' 12import requests 13 14url = "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" 15 16headers = { 17 "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36", 18} 19 20def parse_json(url, params={}): 21 """解析url,得到字典""" 22 response = requests.get(url=url, headers=headers, params=params) 23 return response.json() 24 25content = parse_json(url) 26print(content)


🆗,看来我们是成功获取到数据了。
下面我们来分析下,每一页URL之间的关系。
首先,我们看下其中几个URL
1https://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 2 3https://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 4 5https://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

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


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

分析了页数,那么如果想要循环爬取全部网页,只需进行拼接即可
1for i in range(1,635): 2 params["pageIndex"] = i
二、功能实现

2.1 拼接URL
通过上述的分析,我们知道只需修改每回的pageIndex即可。
首先我们先把需要拼接的部分复制出来

让其转换成字典的方式
1xx="""timestamp: 1603081773061 2countryId: 3cityId: 4bgIds: 5productId: 6categoryId: 7parentCategoryId: 8attrId: 9keyword: 10pageIndex: 2 11pageSize: 10 12language: zh-cn 13area: cn""" 14xx = xx.splitlines() 15params = {} 16for x in xx: 17 print(x.split(":")) 18 params[x.split(":")[0]] = x.split(":")[1] 19 20from pprint import pprint 21pprint(params)

下面就开始代码实现此部分
1params = {'area': ' cn', 2 'attrId': ' ', 3 'bgIds': ' ', 4 'categoryId': ' ', 5 'cityId': ' ', 6 'countryId': ' ', 7 'keyword': ' ', 8 'language': ' zh-cn', 9 'pageIndex': ' 1', 10 'pageSize': ' 10', 11 'parentCategoryId': ' ', 12 'productId': ' ', 13 'timestamp': ' 1602211262824'} 14 15def parse_json(url, params={}): 16 """解析url,得到字典""" 17 response = requests.get(url=url, headers=headers, params=params) 18 return response.json() 19 20def start(): 21 for i in range(1,635): 22 params["pageIndex"] = i 23 24if __name__ == '__main__': 25 start()
2.2 获取数据

由于之前已经讲解过此部分,因此此处只给出代码
1def get_position(data): 2 """获取职位数据""" 3 item = { 4 "postion_name":"",#职位名称 5 "postion_department":"",#职位部门 6 "postion_location":"",#职位所在地 7 "postion_country":"",#职位所在国家 8 "postion_category":"",#职位类别 9 "postion_responsibility":"",#职位职责 10 "postion_url":"",#职位url 11 } 12 data_list = data["Data"]["Posts"] 13 for data in data_list: 14 item["postion_name"] = data["RecruitPostName"] 15 item["postion_department"] = data["BGName"] 16 item["postion_location"] = data["LocationName"] 17 item["postion_country"] = data["CountryName"] 18 item["postion_category"] = data["CategoryName"] 19 item["postion_responsibility"] = data["Responsibility"] 20 item["postion_url"] = data["PostURL"]
三、完整代码

1# encoding: utf-8 2''' 3 @author 李华鑫 4 @create 2020-10-09 9:38 5 Mycsdn:https://buwenbuhuo.blog.csdn.net/ 6 @contact: 459804692@qq.com 7 @software: Pycharm 8 @file: 腾讯招聘.py 9 @Version:1.0 10 11''' 12import requests 13import csv 14 15url = "https://careers.tencent.com/tencentcareer/api/post/Query" 16 17headers = { 18 "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36", 19} 20 21params = {'area': ' cn', 22 'attrId': ' ', 23 'bgIds': ' ', 24 'categoryId': ' ', 25 'cityId': ' ', 26 'countryId': ' ', 27 'keyword': ' ', 28 'language': ' zh-cn', 29 'pageIndex': ' 1', 30 'pageSize': ' 10', 31 'parentCategoryId': ' ', 32 'productId': ' ', 33 'timestamp': ' 1602211262824'} 34 35def parse_json(url, params={}): 36 """解析url,得到字典""" 37 response = requests.get(url=url, headers=headers, params=params) 38 return response.json() 39 40def get_position(data): 41 """获取职位数据""" 42 item = { 43 "postion_name":"",#职位名称 44 "postion_department":"",#职位部门 45 "postion_location":"",#职位所在地 46 "postion_country":"",#职位所在国家 47 "postion_category":"",#职位类别 48 "postion_responsibility":"",#职位职责 49 "postion_url":"",#职位url 50 } 51 data_list = data["Data"]["Posts"] 52 for data in data_list: 53 item["postion_name"] = data["RecruitPostName"] 54 item["postion_department"] = data["BGName"] 55 item["postion_location"] = data["LocationName"] 56 item["postion_country"] = data["CountryName"] 57 item["postion_category"] = data["CategoryName"] 58 item["postion_responsibility"] = data["Responsibility"] 59 item["postion_url"] = data["PostURL"] 60 61 save(item) 62 print(item) 63 print("保存完成") 64 65def save(item): 66 """将数据保存到csv中""" 67 with open("./腾讯招聘.csv", "a", encoding="utf-8") as file: 68 writer = csv.writer(file) 69 writer.writerow(item.values()) 70 71def start(): 72 for i in range(1,635): 73 params["pageIndex"] = i 74 data = parse_json(url,params) 75 get_position(data) 76 77if __name__ == '__main__': 78 start()
四、保存结果


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

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


本文转自 https://buwenbuhuo.blog.csdn.net/article/details/109351951,如有侵权,请联系删除。
