使用selenium进行翻页获取职位链接,再对链接进行解析

会爬取到部分空列表,感觉是网速太慢了,加了time.sleep()还是会有空列表

 from selenium import webdriver
import requests
import re
from lxml import etree
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By class LagouSpider(object):
def __init__(self):
opt = webdriver.ChromeOptions()
# 把chrome设置成无界面模式
opt.set_headless()
self.driver = webdriver.Chrome(options=opt)
self.url = 'https://www.lagou.com/jobs/list_爬虫?px=default&city=北京'
self.headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36'
} def run(self):
self.driver.get(self.url)
while True:
html = ''
links = []
html = self.driver.page_source
links = self.get_one_page_links(html)
for link in links:
print('\n' + link+'\n')
self.parse_detail_page(link) WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'pager_next')))
next_page_btn = self.driver.find_element_by_class_name('pager_next') if 'pager_next_disabled' in next_page_btn.get_attribute('class'):
break
else:
next_page_btn.click()
time.sleep(1) def get_one_page_links(self, html):
links = []
hrefs = self.driver.find_elements_by_xpath('//a[@class="position_link"]')
for href in hrefs:
links.append(href.get_attribute('href'))
return links def parse_detail_page(self, url):
job_information = {}
response = requests.get(url, headers=self.headers) time.sleep(2)
html = response.text
html_element = etree.HTML(html)
job_name = html_element.xpath('//div[@class="job-name"]/@title')
job_description = html_element.xpath('//dd[@class="job_bt"]//p//text()')
for index, i in enumerate(job_description):
job_description[index] = re.sub('\xa0', '', i)
job_address = html_element.xpath('//div[@class="work_addr"]/a/text()')
job_salary = html_element.xpath('//span[@class="salary"]/text()') # 字符串处理去掉不必要的信息
for index, i in enumerate(job_address):
job_address[index] = re.sub('查看地图', '', i)
while '' in job_address:
job_address.remove('') job_information['job_name'] = job_name
job_information['job_description'] = job_description
job_information['job_address'] = job_address
job_information['job_salary'] = job_salary
print(job_information) def main():
spider = LagouSpider()
spider.run() if __name__ == '__main__':
main()

运行结果

爬虫_拉勾网(selenium)的更多相关文章

  1. 爬虫_拉勾网(解析ajax)

    拉勾网反爬虫做的比较严,请求头多添加几个参数才能不被网站识别 找到真正的请求网址,返回的是一个json串,解析这个json串即可,而且注意是post传值 通过改变data中pn的值来控制翻页 job_ ...

  2. python爬虫动态html selenium.webdriver

    python爬虫:利用selenium.webdriver获取渲染之后的页面代码! 1 首先要下载浏览器驱动: 常用的是chromedriver 和phantomjs chromedirver下载地址 ...

  3. scrapy爬虫框架和selenium的配合使用

    scrapy框架的请求流程 scrapy框架? Scrapy 是基于twisted框架开发而来,twisted是一个流行的事件驱动的python网络框架.因此Scrapy使用了一种非阻塞(又名异步)的 ...

  4. Python爬虫之设置selenium webdriver等待

    Python爬虫之设置selenium webdriver等待 ajax技术出现使异步加载方式呈现数据的网站越来越多,当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成,这给定位元素的定位增加 ...

  5. # Python3微博爬虫[requests+pyquery+selenium+mongodb]

    目录 Python3微博爬虫[requests+pyquery+selenium+mongodb] 主要技术 站点分析 程序流程图 编程实现 数据库选择 代理IP测试 模拟登录 获取用户详细信息 获取 ...

  6. 爬虫_淘宝(selenium)

    总体来说代码还不是太完美 实现了js渲染网页的解析的一种思路 主要是这个下拉操作,不能一下拉到底,数据是在中间加载进来的, 具体过程都有写注释 from selenium import webdriv ...

  7. 爬虫基础(三)-----selenium模块应用程序

    摆脱穷人思维 <三> :  培养"目标导向"的思维:  好项目永远比钱少,只要目标正确,钱总有办法解决. 一 selenium模块 什么是selenium?seleni ...

  8. PYTHON 爬虫笔记七:Selenium库基础用法

    知识点一:Selenium库详解及其基本使用 什么是Selenium selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium ...

  9. python3[爬虫实战] 使用selenium,xpath爬取京东手机

    使用selenium ,可能感觉用的并不是很深刻吧,可能是用scrapy用多了的缘故吧.不过selenium确实强大,很多反爬虫的都可以用selenium来解决掉吧. 思路: 入口: 关键字搜索入口 ...

随机推荐

  1. Python-time模块-58

    time 模块: Eva_J import time time.sleep(100) #时间睡眠 print(time.time()) #返回一个以秒为单位的时间 时间模块 和时间有关系的我们就要用到 ...

  2. C. Nastya Is Transposing Matrices

    链接 [https://codeforces.com/contest/1136/problem/C] 题意 给你两个规模一样的矩阵 问是否可以通过不断选取A矩阵的子"方正"转置得到 ...

  3. Python集合及其运算

    目录 集合(set) 集合的创建 集合的操作 集合的运算 子集与父集 集合(set) 集合是由不同可hash的值组成的,里面所有的值都是唯一的,也是无序的 集合的创建 >>>set_ ...

  4. 【转】Restful是什么

    REST的概念是什么 维基百科  表现层状态转换(REST,英文:Representational State Transfer)是Roy Thomas Fielding博士于2000年在他的博士论文 ...

  5. oracle服务端安装与配置

    从oracle官网下载oracle服务端的安装包. 下载下来是两个压缩文件,两个压缩文件都解压(缺一不可)到同一目录下,最后会得到一个database文件夹. 双击database文件夹下的setup ...

  6. java.lang(StringBuffer)

    public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharS ...

  7. [转帖]十分钟快速理解DPI和PPI,不再傻傻分不清!

    十分钟快速理解DPI和PPI,不再傻傻分不清! https://baijiahao.baidu.com/s?id=1605834796518990333&wfr=spider&for= ...

  8. object-fit 属性的用法介绍

    这个要在宽,高都是100%的情况下才能提现 object-fit 属性的用法介绍 fill(不保持纵横比缩放图片,使图片完全适应) contain(保持纵横比缩放图片,使图片的长边能完全显示出来) c ...

  9. eclipse中添加tomcat

    https://blog.csdn.net/Forlogen/article/details/54090335(copy) 为了Java Web的开发,下面我们来安装一下Tomcat服务器,并将其配置 ...

  10. pycharm 安装

    pycharm 1.模板 file->setting->Editor->file and code template->python script->右上方 #!/usr ...