1.背景介绍

selenium通过驱动浏览器,模拟浏览器的操作,进而爬取数据。此外,还需要安装浏览器驱动,相关步骤自行解决。

2.导入库

import csv
import random
import time
from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By

3.去除浏览器识别

option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option('detach', True)

去除浏览器上方的“Chrome正受到自动测试软件的控制”字眼。

4.实例化一个浏览器对象(传入浏览器的驱动程序)

driver = webdriver.Chrome(options=option)

5. 发起请求

driver.get("https://www.51job.com/")
time.sleep(2) #防止加载缓慢,休眠2秒

6.解决特征识别

script = 'Object.defineProperty(navigator, "webdriver", {get: () => false,});'
driver.execute_script(script)

没有出现验证框或验证滑块,说明已经成功屏蔽selenium识别。

7.定位输入框并查找相关职位

driver.find_element(By.XPATH, '//*[@id="kwdselectid"]').click()
driver.find_element(By.XPATH, '//*[@id="kwdselectid"]').clear()
driver.find_element(By.XPATH, '//*[@id="kwdselectid"]').send_keys('老师')
driver.find_element(By.XPATH, '/html/body/div[3]/div/div[1]/div/button').click()
# driver.implicitly_wait(10)
time.sleep(5)
print(driver.current_url)

输入关键词“老师”

8.利用xpath和css选择器提取数据

jobData = driver.find_elements(By.XPATH, '//*[@id="app"]/div/div[2]/div/div/div[2]/div/div[2]/div/div[2]/div[1]/div')
for job in jobData:
jobName = job.find_element(By.CLASS_NAME, 'jname.at').text
time.sleep(random.randint(5, 15) * 0.1)
jobSalary = job.find_element(By.CLASS_NAME, 'sal').text
time.sleep(random.randint(5, 15) * 0.1)
jobCompany = job.find_element(By.CLASS_NAME, 'cname.at').text
time.sleep(random.randint(5, 15) * 0.1)
company_type_size = job.find_element(By.CLASS_NAME, 'dc.at').text
time.sleep(random.randint(5, 15) * 0.1)
company_status = job.find_element(By.CLASS_NAME, 'int.at').text
time.sleep(random.randint(5, 15) * 0.1)
address_experience_education = job.find_element(By.CLASS_NAME, 'd.at').text
time.sleep(random.randint(5, 15) * 0.1) try:
job_welf = job.find_element(By.CLASS_NAME, 'tags').get_attribute('title')
except:
job_welf = '无数据'
time.sleep(random.randint(5, 15) * 0.1) update_date = job.find_element(By.CLASS_NAME, 'time').text
time.sleep(random.randint(5, 15) * 0.1) print(jobName, jobSalary, jobCompany, company_type_size, company_status, address_experience_education, job_welf,
update_date)

因为防止网站防爬,获取数据的同时,让程序休眠随机长度的时间。(根据自我需要设定合适的时间长度)

9.定位页面输入框并实现跳转

xpath定位页码输入框,输入页码,完成跳转

driver.find_element(By.XPATH, '//*[@id="jump_page"]').click()
time.sleep(random.randint(10, 30) * 0.1)
driver.find_element(By.XPATH, '//*[@id="jump_page"]').clear()
time.sleep(random.randint(10, 40) * 0.1)
driver.find_element(By.XPATH, '//*[@id="jump_page"]').send_keys(page)
time.sleep(random.randint(10, 30) * 0.1)
driver.find_element(By.XPATH,
'//*[@id="app"]/div/div[2]/div/div/div[2]/div/div[2]/div/div[3]/div/div/span[3]').click()

10.数据存储

将提取的数据保存进csv中

 with open('wuyou_teacher.csv', 'a', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(
[jobName, jobSalary, jobCompany, company_type_size, company_status, address_experience_education,
job_welf,
update_date])

csv结果如下图:

利用selenium爬取前程无忧招聘数据的更多相关文章

  1. 利用selenium 爬取豆瓣 武林外传数据并且完成 数据可视化 情绪分析

    全文的步骤可以大概分为几步: 一:数据获取,利用selenium+多进程(linux上selenium 多进程可能会有问题)+kafka写数据(linux首选必选耦合)windows直接采用的是写my ...

  2. 利用selenium爬取京东商品信息存放到mongodb

    利用selenium爬取京东商城的商品信息思路: 1.首先进入京东的搜索页面,分析搜索页面信息可以得到路由结构 2.根据页面信息可以看到京东在搜索页面使用了懒加载,所以为了解决这个问题,使用递归.等待 ...

  3. 利用Selenium爬取淘宝商品信息

    一.  Selenium和PhantomJS介绍 Selenium是一个用于Web应用程序测试的工具,Selenium直接运行在浏览器中,就像真正的用户在操作一样.由于这个性质,Selenium也是一 ...

  4. 使用selenium爬取网站动态数据

    处理页面动态加载的爬取 selenium selenium是python的一个第三方库,可以实现让浏览器完成自动化的操作,比如说点击按钮拖动滚轮等 环境搭建: 安装:pip install selen ...

  5. 利用Python爬取朋友圈数据,爬到你开始怀疑人生

    人生最难的事是自我认知,用Python爬取朋友圈数据,让我们重新审视自己,审视我们周围的圈子. 文:朱元禄(@数据分析-jacky) 哲学的两大问题:1.我是谁?2.我们从哪里来? 本文 jacky试 ...

  6. Python爬虫学习(二) ——————爬取前程无忧招聘信息并写入excel

    作为一名Pythoner,相信大家对Python的就业前景或多或少会有一些关注.索性我们就写一个爬虫去获取一些我们需要的信息,今天我们要爬取的是前程无忧!说干就干!进入到前程无忧的官网,输入关键字&q ...

  7. 爬虫(十七):Scrapy框架(四) 对接selenium爬取京东商品数据

    1. Scrapy对接Selenium Scrapy抓取页面的方式和requests库类似,都是直接模拟HTTP请求,而Scrapy也不能抓取JavaScript动态谊染的页面.在前面的博客中抓取Ja ...

  8. python 利用selenium爬取百度文库的word文章

    今天学习如何使用selenium库来爬取百度文库里面的收费的word文档 from selenium import webdriver from selenium.webdriver.common.k ...

  9. python selenium爬取自如租房数据保存到TXT文件

    # -*- coding: utf-8 -*-"""Created on Fri Aug 31  2018 @author: chenlinlab"" ...

  10. 利用selenium爬取豆瓣电影Top250

    这几天在学习selenium,顺便用selenium + python写了一个比较简陋的爬虫,现附上源码,有时间再补充补充: from selenium import webdriver from s ...

随机推荐

  1. 实验:spring-boot整合mybatis-plus实现分页查询的功能

    1.建立基于sping-boot的javaweb工程(java1.8) 按结构建立包 2.POM.XML添加支持mybatis-plus,sql,lombok <!--mybatis-plus的 ...

  2. UniCode 下char*转CString ,利用MultiByteToWideChar进行转换,中文乱码的解决方案

    //计算char *数组大小,以字节为单位,一个汉字占两个字节 int charLen = strlen(sText); //计算多字节字符的大小,按字符计算. int len = MultiByte ...

  3. md5加密js内容

    避免以后都得百度,直接留一份存档 /* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Diges ...

  4. 处理时间转换不正确-Springboot、springclound、feign、http请求

    SpringBoot.SpringCloud.feign.前后端时间解析不正确时,我们可以自定义HttpMessageConverters,以达到我们希望的结果 参考链接:https://www.cn ...

  5. UEC++学习(2)

    第三章 断言 第一节 简单的断言 断言让程序的进程中断,方便程序员发现在哪里发生了问题. AGameBase * GameBase = nullptr; check(false); check(Gam ...

  6. iterm2免密自动登陆服务器

    之前的配置方式出现了less命令查看文本格式紊乱,以及输入的命令也是紊乱的,导致没办法正常使用 以前的配置方式如下: 在iterm2里配置command,如下图 2. online文件如下: #!/u ...

  7. nginx启动命令

    启动加载配置文件 [root@172 sbin]#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 检查配置信息是否正确 ...

  8. selenium定位元素位置-xpath

    xpath定位元素位置: 1.定位父兄节点: <div> <span>我是span1标签</span> <a id="1" href=&q ...

  9. re相关正则表达式(re.sub、re.I 、re.S、re.M)

    re.I 表示忽略大小写 re.S 表示全文匹配 re.M 表示全文拼配行尾段位的字符或者数字,影响^和$ re.sub 表示替换 使用方法: re.sub(pattern, repl, string ...

  10. 洛谷 P1832 A+B Problem(再升级)题解

    START: 2021-08-09 15:28:07 题目链接: https://www.luogu.com.cn/problem/P1832 给定一个正整数n,求将其分解成若干个素数之和的方案总数. ...