案例练习
爬取职位+翻页爬取


指向的只是数字不是url地址


页面分析



ajax接口找寻——Network里面看


打开网址:
第一页:https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1622714101653&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=1622714101653&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=1622714852621&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&**pageIndex=2 **&pageSize=10&language=zh-cn&area=cn
规律是:pageIndex= 页数


创建scrapy
1. scrapy startproject xxx

2.scrapy genspider xxx 域名


爬虫程序文件

start_urls第一页


解析数据
需要找到的数据


在start文件运行


item文件:(在爬虫程序中实例化对象)




加上翻页代码运行:


爬取详情页内容:






同样是ajax数据接口——真正的url是https://careers.tencent.com/tencentcareer/api/post/ByPostId?timestamp=1622731459890&postId=1400451451284627456&language=zh-cn
另一个url是:https://careers.tencent.com/tencentcareer/api/post/ByPostId?timestamp=1622731603080&postId=1369488632871657472&language=zh-cn
唯一不同点是postId
而postId是在每一页的数据接口你的url里


代码逻辑:
这里的two_url是占位的,需要把postId传进去才可以有完整的url


拼接url

这里要用到meta——数据传递,传递PostId来把parse函数获取到的postId值传给另一个爬取详情页的函数detail_content
meta = {‘item’: 需要传递共享的对象}

meta值传递的第一种方法——item =response.meta[‘item’]

meta值传递的第一种方法——item =response.meta.get(‘item’)

在职位详情页拿Requirement当事例,因为在职业url没有这个


结果:


结果:

打开管道pipline并保存

在爬虫文件中因为职责有\n的符号,要去掉

结果


本文详细介绍了使用Python Scrapy框架爬取腾讯职位信息的过程,包括通过AJAX接口寻找翻页和数据请求URL,解析数据,利用meta传递数据,以及爬取详情页内容,并最终通过管道保存结果。
2万+

被折叠的 条评论
为什么被折叠?



