selenium完整爬取拉勾网全国java岗位

爬取全国java岗位

from selenium import webdriver
from lxml import etree
import re
import time


class LagouSpider(object):
    driver_path = r'D:\chromedriver\chromedriver.exe'

    def __init__(self):
        self.driver = webdriver.Chrome(executable_path=self.driver_path)
        self.url = 'https://www.lagou.com/jobs/list_java?labelWords=&fromSearch=true&suginput='

    def run(self):
        self.driver.get(self.url)
        while True:
            source = self.driver.page_source
            self._parse_list_page(source)
            time.sleep(3)

            next_btn = self.driver.find_element_by_xpath("//*[@id='s_position_list']/div[2]/div/span[last()]")
            if 'pager_next pager_next_disabled' not in next_btn.get_attribute('class'):
                next_btn.click()
                time.sleep(2)
            else:
                break
        time.sleep(10)
        self.driver.close()

    def _parse_list_page(self, source):
        html = etree.HTML(source)
        links = html.xpath("//a[@class='position_link']/@href")
        for link in links:
            result = self._parse_detail_page(link)  # 请求每个详情页
            self._show_or_store(result)  # 展示/存储数据

    def _parse_detail_page(self, link): # 爬取详情页【页面切换】
        # self.driver.get(link)
        self.driver.execute_script("window.open('%s')" % link)
        self.driver.switch_to.window(self.driver.window_handles[1])
        print('当前页面:%s' % self.driver.current_url)
        time.sleep(6)   # 每次抓取详情页等待
        source = self.driver.page_source
        html = etree.HTML(source)
        position_name = html.xpath("//div[@class='job-name']/@title")[0]
        company = html.xpath("//*[@id='job_company']/dt/a/div/h3/em/text()")[0].strip()
        temp = []
        result = []
        for i in range(1, 6):
            xpath_url = "/html/body/div[4]/div/div[1]/dd/h3/span[%d]/text()" % i
            item = html.xpath(xpath_url)[0]
            temp.append(re.sub(r'/', '', item))
        position = {
            'name': position_name, 'company': company, 'salary': temp[0], 'address': temp[1],
            'work_year': temp[2], 'education': temp[3], 'desc': temp[4]
        }
        result.append(position)
        # for i in result:
        #     print(i)
        #     print('###' * 50)
        self.driver.close()  # 关闭当前页
        self.driver.switch_to.window(self.driver.window_handles[0])  
        return result

    def _show_or_store(self, result):
        for i in result:
            print(i)
            print('###' * 40)


if __name__ == '__main__':
    spider = LagouSpider()
    spider.run()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值