拉勾网爬虫之利用selenium控制谷歌浏览器爬取职位信息

拉勾网爬虫之利用selenium控制谷歌浏览器爬取职位信息

import time
from lxml import etree
from selenium import webdriver

JOB_LIST = []


class Lagou_Spider(object):
    driver_path = r"C:\ChromeDriver\chromedriver.exe"

    def __init__(self):
        self.driver = webdriver.Chrome(executable_path=self.driver_path)
        self.url = 'https://www.lagou.com/jobs/list_%E4%BE%9B%E5%BA%94%E9%93%BE?labelWords=&fromSearch=true&suginput='

    def run(self):
        self.driver.get(self.url)
        while True:
            source = self.driver.page_source
            self.parse_page_url(source)
            next_btn = self.driver.find_element_by_class_name("pager_next ")
            # 查看是否已到最后一页,如是,则爬取完成
            if "pager_next pager_next_disabled" in next_btn.get_attribute("class"):
                break
            else:
                next_btn.click()
            time.sleep(1)

    def parse_page_url(self, source):
        html = etree.HTML(source)
        detail_urls = html.xpath('//a[@class="position_link"]/@href')
        for detail_url in detail_urls:
            self.parse_detail_url(detail_url)
        time.sleep(2)

    def parse_detail_url(self, detail_url):
        self.driver.execute_script("window.open('%s')" % detail_url)
        self.driver.switch_to_window(self.driver.window_handles[1])
        html = etree.HTML(self.driver.page_source)
        title = html.xpath('//h1[@class="name"]/text()')[0].strip()
        company = html.xpath('//h4[@class="company"]/text()')[0].strip()
        salary = html.xpath('//h3/span[@class="salary"]/text()')[0].strip()
        address = html.xpath('//h3/span/text()')[1].replace('/', '').strip()
        years = html.xpath('//h3/span/text()')[2].replace('/', '').strip()
        education = html.xpath('//h3/span/text()')[3].replace('/', '').strip()
        status = html.xpath('//h3/span/text()')[4].strip()
        description = ''.join(html.xpath('//div[@class="job-detail"]//p/text()')).strip()
        lables = '|'.join(html.xpath('//ul[@class="position-label clearfix"]/li/text()'))  # 将得到的列表以'|'连接起来并转换为文本
        job_dic = {}
        job_dic = {'职位': title,
                   '标签': lables,
                   '公司': company,
                   '薪资': salary,
                   '地址': address,
                   '工作经验': years,
                   '学历': education,
                   '兼全职': status,
                   '详细要求': description}
        print(job_dic)
        JOB_LIST.append(job_dic)
        self.driver.close()
        self.driver.switch_to_window(self.driver.window_handles[0])


if __name__ == '__main__':
    lagou = Lagou_Spider()
    lagou.run()

总结:
由于拉钩的反扒较严,本想从分析Ajax接口来直接爬取,但是headers试了好几次没成功就直接放弃了。采用selenium的方法较为稳定,不易被识别为爬虫,不过这个代码爬了两页之后就要求登录了,估计要加入cookie再试。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值