案例练习
爬取职位+翻页爬取
指向的只是数字不是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没有这个
结果: