前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python爬虫 senlenium爬取拉勾网招聘数据

python爬虫 senlenium爬取拉勾网招聘数据

作者头像
叶庭云
修改2021-01-23 13:35:55
1.4K0
修改2021-01-23 13:35:55
举报
文章被收录于专栏:Python进阶之路Python进阶之路

文章目录

一、基本思路

目标url:https://www.lagou.com/

用selenium爬虫实现,输入任意关键字,比如 python 数据分析,点击搜索,得到的有关岗位信息,爬取下来保存到Excel。

有30页,每个页面有15条招聘信息。

二、selenium爬虫

代码语言:txt
复制
from selenium import webdriver
import time
import logging
import random
import openpyxl

wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(['job_name', 'company_name', 'city','industry', 'salary', 'experience_edu','welfare','job_label'])
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s')


def search_product(key_word):
    browser.find_element_by_id('cboxClose').click()     # 关闭让你选城市的窗口
    time.sleep(2)
    browser.find_element_by_id('search_input').send_keys(key_word)  # 定位搜索框 输入关键字
    browser.find_element_by_class_name('search_button').click()     # 点击搜索
    browser.maximize_window()    # 最大化窗口
    time.sleep(2)
    browser.find_element_by_class_name('body-btn').click()    # 关闭弹窗  啥领取红包窗口
    time.sleep(random.randint(1, 3))
    browser.execute_script("scroll(0,3000)")      # 下拉滚动条
    get_data()           # 调用抓取数据的函数
    # 模拟点击下一页   翻页爬取数据  每爬取一页数据  休眠   控制抓取速度  防止被反爬 让输验证码
    for i in range(29):
        browser.find_element_by_class_name('pager_next ').click()
        time.sleep(1)
        browser.execute_script("scroll(0,3000)")
        get_data()
        time.sleep(random.randint(3, 5))


def get_data():
    items = browser.find_elements_by_xpath('//*[@id="s_position_list"]/ul/li')
    for item in items:
        job_name = item.find_element_by_xpath('.//div[@class="p_top"]/a/h3').text
        company_name = item.find_element_by_xpath('.//div[@class="company_name"]').text
        city = item.find_element_by_xpath('.//div[@class="p_top"]/a/span[@class="add"]/em').text
        industry = item.find_element_by_xpath('.//div[@class="industry"]').text
        salary = item.find_element_by_xpath('.//span[@class="money"]').text
        experience_edu = item.find_element_by_xpath('.//div[@class="p_bot"]/div[@class="li_b_l"]').text
        welfare = item.find_element_by_xpath('.//div[@class="li_b_r"]').text
        job_label = item.find_element_by_xpath('.//div[@class="list_item_bot"]/div[@class="li_b_l"]').text
        data = f'{job_name},{company_name},{city},{industry},{salary},{experience_edu},{welfare},{job_label}'
        logging.info(data)
        sheet.append([job_name, company_name, city,industry, salary, experience_edu, welfare, job_label])


def main():
    browser.get('https://www.lagou.com/')
    time.sleep(random.randint(1, 3))
    search_product(keyword)
    wb.save('job_info.xlsx')


if __name__ == '__main__':
    keyword = 'Python 数据分析'
    # chromedriver.exe的路径
    chrome_driver = r'D:\python\pycharm2020\chromedriver.exe'
    options = webdriver.ChromeOptions()
    # 关闭左上方 Chrome 正受到自动测试软件的控制的提示
    options.add_experimental_option('useAutomationExtension', False)
    options.add_experimental_option("excludeSwitches", ['enable-automation'])
    browser = webdriver.Chrome(options=options, executable_path=chrome_driver)
    main()
    browser.quit()

爬虫运行,成功爬取数据并保存到Excel,运行结果如下:

三、查看数据

作者:叶庭云 微信公众号:修炼Python CSDN:https://yetingyun.blog.csdn.net/ 本文仅用于交流学习,未经作者允许,禁止转载,更勿做其他用途,违者必究。 觉得文章对你有帮助、让你有所收获的话,期待你的点赞呀,不足之处,也可以在评论区多多指正。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、基本思路
  • 二、selenium爬虫
  • 三、查看数据
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档