使用selenium抓取boss直聘

# -*- coding:utf-8 -*-
from selenium import webdriver
from  lxml import etree
import time

class BossSpider(object):
    def __init__(self):
        self.start_url = 'https://www.zhipin.com/job_detail/?query=python&city=101020100&industry=&position='
        self.driver = webdriver.Chrome()

    def get_page_url(self,source):
        html = etree.HTML(source)
        position_href_links = html.xpath("//div[@class='job-list']/ul/li//div[@class='info-primary']/h3/a/@href")
        for href in position_href_links:
            position_url = "https://www.zhipin.com" + href
            print(position_url)
            self.get_detail_info(position_url)
            time.sleep(2)
        time.sleep(2)

    def get_detail_info(self,url):
        self.driver.execute_script('window.open("%s")'%url)
        self.driver.switch_to.window(self.driver.window_handles[1])
        source = self.driver.page_source
        self.parse_detail_page(source)
        self.driver.close()
        self.driver.switch_to.window(self.driver.window_handles[0])

    def parse_detail_page(self,source):
        html = etree.HTML(source)
        position_info = {}
        position_info["company"] = html.xpath("//div[@class='company-info']/a[2]/text()")[0].strip()
        # position_info["salary"] = html.xpath("//div[@class='info-primary']/div[@class='name']/span/text()]")[0].strip()
        position_info["salary"] = html.xpath("//span[@class='salary']/text()")[0].strip()
        position_info["city"] = html.xpath("//div[@class='info-primary']/p//text()")[0]
        position_info["work_years"] = html.xpath("//div[@class='info-primary']/p//text()")[1]
        position_info["education"] = html.xpath("//div[@class='info-primary']/p//text()")[2]
        position_info["desc"] = "".join(html.xpath("//div[@class='job-sec']/div/text()")).strip()
        print(position_info)

    def run(self):
        self.driver.get(self.start_url)
        while True:
            source =self.driver.page_source
            self.get_page_url(source)
            next_btn = self.driver.find_element_by_xpath("//div[@class='page']/a[last()]")
            if "next disabled" in next_btn.get_attribute("class"):
                pass
            else:
                next_btn.click()
                time.sleep(2)
            time.sleep(5)

if __name__ == '__main__':
    BossSpider().run()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值