拉勾作为爬虫学习必被骚扰的网站,反爬策略做的还是很厉害的。
爬取拉勾数据运营相关职位信息,有两种方式:
一种是通过搜索“数据运营”,这时页面是通过json来传递数据
另一种是直接在首页点击“数据运营”标签,这时页面是直接代码渲染出来的,可以采用xpath方法来爬取数据,本文就是采用这种方式。
通过分析页面,我们要爬取的数据包括如下几个方面:职位名称、地址、公司名称、工资范围、经验学历、公司概况
代码如下:
items.py
import scrapy
class LagouscrapyItem(scrapy.Item):
# define the fields for your item here like:
name = scrapy.Field()#职位名称
price = scrapy.Field()#工资区间
company = scrapy.Field()#公司名称
address = scrapy.Field()#地址
industry = scrapy.Field()#公司简介
experience = scrapy.Field()#经验学历
settings.py(在这里设置UA和cookie,突破拉勾的反爬,如果想大规模爬取,还可以增加IP池)
BOT_NAME =