python + selenium 爬取猎聘招聘网

  Selenium 本是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,模拟用户操作。而这一特性为爬虫开发提供了一个选择及方向,由于其本身依赖于浏览器,所以使用Python的selenium库的前提是:需要下载相应的浏览器驱动程序,这里附上Chromedriver的下载地址:chromedriver

爬取猎聘职位标题 及 标题链接

代码块

from selenium import webdriver

启动谷歌浏览器
driver=webdriver.Chrome()

driver.implicitly_wait(10) # 隐式等待

爬取猎聘网
driver.get(url=‘https://www.liepin.com/’)

定位输入框 并输入 python
driver.find_element_by_xpath(’//*[@id=“home”]/div[3]/div[1]/div/div/form/div/div/input’).send_keys(‘python’)

定位搜索按钮 并点击
driver.find_element_by_xpath(’//[@id=“home”]/div[3]/div[1]/div/div/form/div/button’.click()

效果

python + selenium 爬取猎聘招聘网

爬取职位标题
name = driver.find_elements_by_xpath(’//*[@id=“sojob”]/div[2]/div/div[1]/div[1]/ul/li/div/div[1]/h3/a’)

因为是elements 爬取内容是个列表 需要遍历

listall = []
for j in range(len(name)):
dict = {}
text 取出文本
dict[‘name’] = name[j].text

get_attribute(‘href’) 取出href属性中的超链接
dict[‘url’] = name[j].get_attribute(‘href’)

python + selenium 爬取猎聘招聘网

输出结果

python + selenium 爬取猎聘招聘网

点击链接进入对应的详情页

python + selenium 爬取猎聘招聘网

接下来可以用超链接 爬取职位描述等信息 在这里我就不爬取了

selenium常用方法

常用方法总结
python + selenium 爬取猎聘招聘网


往期推荐

python中对list去重的方法

flask框架之分页

django框架之分页

机器学习之绘图

爬虫学习之selenium(一)