记录抓取某直聘网站

  

近期有朋友让我帮抓一下某个直聘网站的招聘岗位,闲来无事就试了一下。

  

考虑到这种网站肯定是有反爬机制,于是使用硒+铬的方式抓取

  

用到的主要工具:

  

python3.5   

硒   

scrapy   

由于[网站的数据跟单(http://www.gendan5.com/tech.html)是可以按照地市来查询的,所以先访问该网站支持的城市划分

  

使用scrapy的self.start_urls进行请求

  

自我。start_urls=(“https://www.zhipin.com/wapi/zpCommon/data/city.json”)

  1

  

同时使用硒请求该网站主页

  

self.driver.get (“https://www.zhipin.com/?

  1

  

后来发现网站可以识别硒、不返回数据,于是添加

  

选项=webdriver.ChromeOptions ()

  

选项。add_experimental_option (“excludeSwitches”,['实现自动化'])

  

自我。司机=webdriver.Chrome(选项=选项)

  

将程序设置为开发者模式,数据可以正常请求到

  

接下来就是解析支持搜索的城市名,并且汇总成我们能使用的数据格式

  
 <代码> dic={}
  
  json_text=json.loads (response.text) (“zpData”) (“cityList”)
  
  因为我在范围(len (json_text)):
  
  #获取到各个省的名称,并且作为字典的键名赋值
  
  ?json_text[我]['名字']
  
  ?json_text[我][' subLevelModelList ']
  
  dic.setdefault(省,[])
  
  北京市=[]
  
  #分类直辖市和地级市,并归类到字典的值
  
  如果provinces.__len__()在1:
  
  二世的范围(len(省):
  
  城市=省[2](“名字”)
  
  citys.append(市)
  
  其他:
  
  城市=省
  
  citys.append(市)
  
  迪拜国际资本(省)=城市 
  

准备工作完成了,接下来就是请求数据了

  
 <代码> self.driver.find_element_by_xpath ('//* [@ id="包装"]/div [3]/div/div/div[1]/形式/div [2]/p/输入”).send_keys(的需要查询的岗位”)#主页搜索框,过度用
  
  睡眠(2)
  
  self.driver.find_element_by_xpath ('//* [@ id="包装"]/div [3]/div/div/div[1]/表单/按钮).click ()
  
  睡眠(2) 
  

到这里,程序算是进入了正轨,直接贴上代码。如下:

  

: - 编码utf - 8 -

  进口scrapy

  进口json

  进口re

  从scrapy

。蜘蛛进口CrawlSpider

  

从进口睡眠时间

  

从. .项目导入ZhaopinBossZhipinItem

  从scrapy

。选择器选择器进口

  进口importlib

  

进口随机   

从硒进口webdriver

  

导入系统   

importlib.reload (sys)

  

类ZP_boss (CrawlSpider):

  

老板比;各地口腔招聘

  
 <代码> name="老板"
  
  custom_settings={
  
  “ITEM_PIPELINES”: {zhaopin_bosszhipin.pipelines。}ZhaopinBossPipeline”: 300年,
  
  “scrapy.downloadermiddlewares.cookies。CookiesMiddleware”: 1、
  
  “DOWNLOAD_DELAY”: 0.5,
  
  “MYEXT_ENABLED”:真的
  
  }
  
  def __init__(自我):
  
  超级(ZP_boss自我). __init__ ()
  
  自我。allowed_domains=(“https://www.baidu.com”) #过滤的url
  
  自我。start_urls=(“https://www.zhipin.com/wapi/zpCommon/data/city.json”) #访问网页支持搜索的城市
  
  选择=webdriver.ChromeOptions ()
  
  选项。add_experimental_option (“excludeSwitches”,['实现自动化'])
  
  自我。司机=webdriver.Chrome(选项=选项)
  
  self.driver.maximize_window() #浏览器设置成页面最大化
  
  self.driver.get (“https://www.zhipin.com/?
  
  def解析(自我,反应):
  
  dic={}
  
  json_text=json.loads (response.text) (“zpData”) (“cityList”)
  
  因为我在范围(len (json_text)):
  
  #获取到各个省的名称,并且作为字典的键名赋值
  
  ?json_text[我]['名字']
  
  ?json_text[我][' subLevelModelList ']
  
  dic.setdefault(省,[])
  
  北京市=[]
  
  #分类直辖市和地级市,并归类到字典的值
  
  如果provinces.__len__()在1:
  
  二世的范围(len(省):
  
  城市=省[2](“名字”)
  
  citys.append(市)
  
  其他:
  
  城市=省
  
  citys.append(市)
  
  迪拜国际资本(省)=城市
  
  self.driver.find_element_by_xpath ('//* [@ id="包装"]/div [3]/div/div/div[1]/形式/div [2]/p/输入”).send_keys (python) #主页搜索框,过度用
  
  睡眠(2)
  
  self.driver.find_element_by_xpath ('//* [@ id="包装"]/div [3]/div/div/div[1]/表单/按钮).click ()
  
  睡眠(2)
  
  对新城东区dic.keys(): #循环抓取到的省名
  
  cts=dic][箴言#单个省或者直辖市包含的所有城市
  
  ct在cts: #单个城市名
  
  查询='搜索的岗位' + ct
  
  self.driver.find_element_by_xpath ('//p [@class=" ipt-wrap "]/输入[@ name="查询"]).clear ()
  
  #睡眠(0.1)
  
  self.driver.find_element_by_xpath ('//p [@class=" ipt-wrap "]/输入[@ name="查询"]).send_keys(查询)
  
  睡眠(0.2)
  
  self.driver。find_element_by_xpath('//按钮[@class=" btn btn-search "]”) .click() #点击查询数据
  
  睡眠(1)
  
  #=源选择器(文本=self.driver.page_source)
  
  panduan=True
  
  虽然panduan: #循环翻页
  
  苏=选择器(文本=self.driver.page_source)
  
  link_lens=sou.xpath ('//* [@ id="主"]/div/div [2]/ul/李’).extract() #获取当前页面所有李的标签,一个标签就是一条招聘数据
  
  #分解出当前页面每一个李标签,并获取到部分数据
  
  在link_lens link_text:
  
  选?选择器(文本=link_text)
  
  #招聘单位
  
  . join (sel.xpath公司=" ('//div [@class=" company-text "]/h4//text()的).extract ()) .strip ()
  
  #城市
  
  城市=ct
  
  #学历要求
  
  . join (sel.xpath教育=" ('//div [@class=" info-primary "]/p/text () [3]”) .extract ()) .strip ()
  
  #工作经验
  
  . join (sel.xpath经验=" ('//div [@class=" info-primary "]/p/text () [2]”) .extract ()) .strip ()
  
  #获取数据的城市地址
  
  adrs_text=sel.xpath ('//p/text () ') .extract ()
  
  如果adrs_text: #加这个判断是为了保证有城市数据,有时候网页会抽风导致下标越界或空对象没有组()方法的错
  
  adr=re.search ((\ w + ?) \年代”、“. join (adrs_text [0])) .group () .strip() #匹配出当前招聘所在城市名
  
  如果adr !=ct: #如果没有匹配数据,网站会把该省的其他市数据返回,筛选掉这部分数据,只做精准匹配
  
  panduan=False
  
  继续
  
  其他:
  
  通过=re.search (“\ (\ w + ?) \年代”、“. join (adrs_text[0])) #城市的区
  
  如果是:
  
  面积=are.group () .strip ()
  
  其他:
  
  面积="
  
  main_url=' https://www.zhipin.com '
  
  . join (sel.xpath link_hrefhttps://www.yisu.com/zixun/=?'//div [@class=" info-primary "]/h4 [@class="名称"]//@href’) .extract ()) .strip ()
  
  url=main_url + link_href
  
  #获取详情页的索引值
  
  . join (sel.xpath href_index=" ('//div [@class=" info-primary "]/h4 [@class="名称"]//@data-index’) .extract ()) .strip ()
  
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null

记录抓取某直聘网站