6.1.爬取第一页的职位信息

第一页职位信息

from selenium import webdriver
from lxml import etree
import re
import time class LagouSpider(object):
def __init__(self):
self.driver = webdriver.Chrome()
#python职位
self.url = 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
self.position = [] def run(self):
self.driver.get(self.url)
source = self.driver.page_source
self.parse_list_page(source) def parse_list_page(self,source):
html = etree.HTML(source)
links = html.xpath("//a[@class='position_link']/@href")
#每一页的所有职位的详情url
for link in links:
self.request_detail_page(link)
time.sleep(1) def request_detail_page(self,url):
self.driver.get(url)
#获取职位详情页的源代码
source = self.driver.page_source
self.parse_detail_page(source) def parse_detail_page(self,source):
html = etree.HTML(source)
position_name = html.xpath("//span[@class='name']/text()")[0]
job_request_spans = html.xpath("//dd[@class='job_request']//span")
salary = job_request_spans[0].xpath('.//text()')[0].strip()
city = job_request_spans[1].xpath('.//text()')[0].strip()
city = re.sub(r"[\s/]","",city)
work_years = job_request_spans[2].xpath('.//text()')[0].strip()
work_years = re.sub(r"[\s/]","",work_years)
education = job_request_spans[3].xpath('.//text()')[0].strip()
education = re.sub(r"[\s/]","",education)
desc = "".join(html.xpath("//dd[@class='job_bt']//text()")).strip()
position = {
'name':position_name,
'salary':salary,
'city': city,
'work_years': work_years,
'education': education,
'desc': desc,
}
self.position.append(position)
print(position)
print('-'*200) if __name__ == '__main__':
spider = LagouSpider()
spider.run()

6.2.爬取所有页的职位信息

from selenium import webdriver
from lxml import etree
import re
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):
self.driver = webdriver.Chrome()
#python职位
self.url = 'https://www.lagou.com/jobs/list_python?city=%E5%8C%97%E4%BA%AC&cl=false&fromSearch=true&labelWords=&suginput='
self.position = [] def run(self):
self.driver.get(self.url)
while True:
source = self.driver.page_source
WebDriverWait(driver=self.driver,timeout=20).until(
EC.presence_of_element_located((By.XPATH,"//div[@class='pager_container']/span[last()]"))
)
self.parse_list_page(source)
#点“下一页”
next_btn = self.driver.find_element_by_xpath(
"//div[@class='pager_container']/span[last()]")
if "pager_next_disabled" in next_btn.get_attribute("class"):
break
else:
next_btn.click()
time.sleep(1) def parse_list_page(self,source):
html = etree.HTML(source)
links = html.xpath("//a[@class='position_link']/@href")
#每一页的所有职位的详情url
for link in links:
self.request_detail_page(link)
time.sleep(1) def request_detail_page(self,url):
# self.driver.get(url)
self.driver.execute_script("window.open('%s')"%url)
self.driver.switch_to.window(self.driver.window_handles[1]) WebDriverWait(driver=self.driver,timeout=20).until(
EC.presence_of_element_located((By.XPATH,"//div[@class='job-name']/span[@class='name']"))
)
#获取职位详情页的源代码
source = self.driver.page_source
self.parse_detail_page(source)
#关闭当前详情页,并且切换到列表页
self.driver.close()
self.driver.switch_to.window(self.driver.window_handles[0]) def parse_detail_page(self,source):
html = etree.HTML(source)
position_name = html.xpath("//span[@class='name']/text()")[0]
job_request_spans = html.xpath("//dd[@class='job_request']//span")
salary = job_request_spans[0].xpath('.//text()')[0].strip()
city = job_request_spans[1].xpath('.//text()')[0].strip()
city = re.sub(r"[\s/]","",city)
work_years = job_request_spans[2].xpath('.//text()')[0].strip()
work_years = re.sub(r"[\s/]","",work_years)
education = job_request_spans[3].xpath('.//text()')[0].strip()
education = re.sub(r"[\s/]","",education)
desc = "".join(html.xpath("//dd[@class='job_bt']//text()")).strip()
company_name = html.xpath("//h2[@class='fl']/text()")[0].strip()
position = {
'name':position_name,
'company_name':company_name,
'salary':salary,
'city': city,
'work_years': work_years,
'education': education,
'desc': desc,
}
self.position.append(position)
print(position)
print('-'*200) if __name__ == '__main__':
spider = LagouSpider()
spider.run()

21天打造分布式爬虫-Selenium爬取拉钩职位信息(六)的更多相关文章

  1. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  2. 通俗易懂的分析如何用Python实现一只小爬虫,爬取拉勾网的职位信息

    源代码:https://github.com/nnngu/LagouSpider 效果预览 思路 1.首先我们打开拉勾网,并搜索"java",显示出来的职位信息就是我们的目标. 2 ...

  3. ruby 爬虫爬取拉钩网职位信息,产生词云报告

    思路:1.获取拉勾网搜索到职位的页数 2.调用接口获取职位id 3.根据职位id访问页面,匹配出关键字 url访问采用unirest,由于拉钩反爬虫,短时间内频繁访问会被限制访问,所以没有采用多线程, ...

  4. python3爬虫-通过requests获取拉钩职位信息

    import requests, json, time, tablib def send_ajax_request(data: dict): try: ajax_response = session. ...

  5. selelinum+PhantomJS 爬取拉钩网职位

    使用selenium+PhantomJS爬取拉钩网职位信息,保存在csv文件至本地磁盘 拉钩网的职位页面,点击下一页,职位信息加载,但是浏览器的url的不变,说明数据不是发送get请求得到的. 我们不 ...

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

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

  7. python3编写网络爬虫16-使用selenium 爬取淘宝商品信息

    一.使用selenium 模拟浏览器操作爬取淘宝商品信息 之前我们已经成功尝试分析Ajax来抓取相关数据,但是并不是所有页面都可以通过分析Ajax来完成抓取.比如,淘宝,它的整个页面数据确实也是通过A ...

  8. Python爬虫项目--爬取自如网房源信息

    本次爬取自如网房源信息所用到的知识点: 1. requests get请求 2. lxml解析html 3. Xpath 4. MongoDB存储 正文 1.分析目标站点 1. url: http:/ ...

  9. 爬虫—Selenium爬取JD商品信息

    一,抓取分析 本次目标是爬取京东商品信息,包括商品的图片,名称,价格,评价人数,店铺名称.抓取入口就是京东的搜索页面,这个链接可以通过直接构造参数访问https://search.jd.com/Sea ...

随机推荐

  1. 100-days: fifteen

    Title: Disney(迪士尼) moves from behemoth to colossus with closing(使…结束,使停止) of Fox(福克斯) deal(商业上的交易/协议 ...

  2. FortiGate外网IPSec链路及运维专线链路到个别网段不通

    1.现状: 如图,用户网段有192.168.50.0/24.192.168.51.0/24和192.168.52.0/24.192.168.53.0/24.在防火墙上有静态路由到运维专线的10.160 ...

  3. MAC shell ps 命令详解(转)

    ps命令为我们提供了一次性的查看进程结果,它所提供的查看结果不是动态连续的:如果想对进程时间监控,应该用top工具 Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当 ...

  4. Java第五次实验

    Java第五次实验--网络编程与安全 任务一: 一.实验要求: 两人一组结对编程: 参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA 结对实现 ...

  5. SpringBoot小新手。

    2018-09-27 最近在学习SpringBoot:教材 先是在https://start.spring.io/下载了工程.demo.zip 下载之后,导入Eclipse工程,pom.xml里面的& ...

  6. NFS服务简介与配置

    NFS简介 NFS特点 NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源 在NFS的应用中, ...

  7. oracle 为什么没有权限的用户也可以用sysdba登录

    我随便创建了一个用户,create user lisi identified by lisi; 当我用sqlplus登录的时候: cmd ->  sqlplus lisi/lisi  进不去   ...

  8. IDEA下Spring Boot显示Run Dashboard面板

    在 .idea/workspace.xml 文件中找到 <component name="RunDashboard"> <option name="ru ...

  9. VMware虚拟机网络设置

    背景介绍 在用 VMware workstation 安装好虚拟机后,需要给虚拟机配置网络,配置网络的方法有桥接.NAT.    采用桥接的方法需要占据物理机网段的ip地址,可能会与物理机同一网段的其 ...

  10. Solidity合约记录——(一)如何寻找以太坊真实Solidity源码

    在自主学习Solidity智能合约的过程中,第一份入手资料无疑是官方文档.感谢前辈们还能提供出文档的中文翻译,作为我入门的第一手资料:文末附上有用的学习链接{持续更新中} 阅读完基础文档同时上手合约后 ...