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. zabbix3.4 监控ESXI6.7

    一.ESXI WEB界面 管理--高级配置启用 键 Config.HostAgent.plugins.solo.enableMob 访问:https://10.81.1.219/mob/?moid=h ...

  2. PowerScript语言基础

    注释: 以 "//" 开头,其后书写注释内容,常用于单行注释. "/-/"中间的部分为注释,便于多行说明. //这是一个单行注释 INTEGER I I = I ...

  3. python import hashllb

    http://www.cnblogs.com/alex3714/articles/5161349.html 用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224 ...

  4. 获取MessageBox按钮本地字符串(OK、Cancel、Yes、No等)

    问题仍然由定制MessageBox引发. 定制MessageBox,虽加入自定义些东西,但仍然希望,最大限度接近系统原生框.碰到的问题,就是其钮文本. 即如MessageBox.Show()之Mess ...

  5. input text 只能输入数字

    添加 onkeyup="value=value.replace(/[^\d]/g,'')"

  6. 认识正则RegExp;

    1.什么是正则??? 就是一条规则,用于检验字符串的格式,目标就是字符串. *只要是表单提交的数据都是字符串 2.正则的定义??? (1)var reg=new RegExp() (2)var reg ...

  7. No write since last change (add ! to override)

    故障现象: 使用vim修改文件报错,系统提示如下: E37: No write since last change (add ! to override) 故障原因: 文件为只读文件,无法修改. 解决 ...

  8. Python(四) 列表元组

  9. # 2019-2020-3 《Java 程序设计》第四周总结

    2019-2020-3 <Java 程序设计>第四周知识总结 第五章:继承 1.定义继承关系的语法结构: [修饰符] class 子类名 extends 父类名 { 类体定义 } 父类中应 ...

  10. Python11/23--mysql用户管理/pymysql

    1.mysql用户管理 定义:数据安全是很重要的,不能随便分配root账户,应该按照不同开发岗位分配不同的账户和权限 mysql中将用户相关的数据放在mysql库中 user→db→tables_pr ...