目的:本文主要描述如何使用Java+selenium爬取58同城招聘页,并记录指定职位的招聘公司名保存到本地

一、首先创建一个maven工程,配置依赖包

 1 <dependencies>
2
3 <!-- selenium-java -->
4 <dependency>
5 <groupId>org.seleniumhq.selenium</groupId>
6 <artifactId>selenium-java</artifactId>
7 <version>3.4.0</version>
8 </dependency>
9
10 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
11 <dependency>
12 <groupId>org.apache.poi</groupId>
13 <artifactId>poi</artifactId>
14 <version>3.9</version>
15 </dependency>
16
17 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
18 <dependency>
19 <groupId>org.apache.poi</groupId>
20 <artifactId>poi-ooxml</artifactId>
21 <version>3.9</version>
22 </dependency>
23 </dependencies>

二、开始写入自动化测试代码

 1 import org.apache.poi.xssf.usermodel.XSSFSheet;
2 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
3 import org.openqa.selenium.By;
4 import org.openqa.selenium.WebDriver;
5 import org.openqa.selenium.WebElement;
6 import org.openqa.selenium.chrome.ChromeDriver;
7 import java.io.FileInputStream;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.util.concurrent.TimeUnit;
11
12
13 public class Zhaopin {
14 public static void main(String[] args) throws InterruptedException, IOException {
15 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
16 WebDriver driver = new ChromeDriver();
17 driver.manage().window().maximize(); //窗口最大化
18 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
19 driver.get("http://www.58.com/"); //输入网址
20
21 driver.findElement(By.xpath("//*[@id=\"commonTopbar_ipconfig\"]/a[1]")).click();
22 WebElement input = driver.findElement(By.xpath("//*[@id=\"selector-search-input\"]"));
23 input.sendKeys("深圳"); //切换城市,打开默认是本地
24
25 driver.findElement(By.xpath("//*[@id=\"selector-search-btn\"]")).click();
26 driver.findElement(By.xpath("/html/body/div[3]/div[1]/div[1]/div/div[3]/div[1]/h2/a")).click();//打开招聘
27 Thread.sleep(1000);
28 String SecondtHandle = driver.getWindowHandle(); //首先得到最先的窗口 权柄
29 for (String winHandle1 : driver.getWindowHandles()) { //得到浏览器所有窗口的权柄为Set集合,遍历
30 if (!winHandle1.equals(SecondtHandle)) { //如果为 最先的窗口 权柄跳出
31 driver.close();
32 driver.switchTo().window(winHandle1); //如果不为 最先的窗口 权柄,将 新窗口的操作权柄 给 driver
33 System.out.println(driver.getCurrentUrl()); //打印是否为新窗口
34 }
35 }
36
37
38 FileInputStream fis = new FileInputStream("D:\\Desktop\\test.xlsx");//创建输入流,获取本地文件
39 XSSFWorkbook workbook=new XSSFWorkbook(fis); //创建工作簿,将数据读入到workbook中
40 XSSFSheet sheet1 = workbook.getSheet("Sheet1");
41 String index= sheet1.getRow(0).getCell(0).getStringCellValue(); //读取文件内容,为下文做索引
42
43 WebElement input1 = driver.findElement(By.xpath("//*[@id=\"keyword1\"]"));
44 input1.sendKeys(index);
45 driver.findElement(By.xpath("//*[@id=\"searJob\"]/strong")).click(); //搜索关键词
46
47 for (int i=1;i<100;i++){
48 String name=driver.findElement(By.xpath("//*[@id=\"list_con\"]/li["+i+"]/div[2]/div/a")).getAttribute("title");
49 //查找每一条记录的title值
50 sheet1.createRow(i).createCell(0).setCellValue(name);
51 } //遍历该页面所有公司名并写入excel
52
53 sheet1.createRow(0).createCell(0).setCellValue(index);
54 FileOutputStream os = new FileOutputStream("D:\\Desktop\\2.xlsx");//创建一个向指定位置写入文件的输出流
55 workbook.write(os);//向指定的文件写入excel
56 os.close();//关闭流
57 driver.close();//关闭浏览器
58 }
59 }

三、运行结果,读取本地的test.xlsx文件内容,将结果作为搜索条件

Java+selenium自动爬取网站内容并写入本地的更多相关文章

  1. selenium自动爬取网易易盾的验证码

    我们在爬虫过程中难免会遇到一些拦路虎,比如各种各样的验证码,时不时蹦出来,这时候我们需要去识别它来继续我们的工作,接下来我将爬取网一些滑动验证码,然后通过百度的EasyDL平台进行数据标注,创建模型, ...

  2. 利用Jsoup包爬取网站内容

    一 Jsoup包 下载链接:http://download.csdn.net/detail/u014000832/7994245 二 爬取搜狐新闻网站标题等内容 package com.test1; ...

  3. 用selenium 自动爬取某一本小说章节及其内容,并存入数据库中

    from selenium import webdriver import pymysql from selenium.webdriver.support.ui import WebDriverWai ...

  4. python爬取网站视频保存到本地

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: Woo_home PS:如有需要Python学习资料的小伙伴可以加点 ...

  5. Java爬虫实践--爬取CSDN网站图片为例

    实现的效果,自动在工程下创建Pictures文件夹,根据网站URL爬取图片,层层获取.在Pictures下以网站的层级URL命名文件夹,用来装该层URL下的图片.同时将文件名,路径,URL插入数据库, ...

  6. 使用Selenium爬取网站表格类数据

    本文转载自一下网站:Python爬虫(5):Selenium 爬取东方财富网股票财务报表 https://www.makcyun.top/web_scraping_withpython5.html 需 ...

  7. Python3.x:Selenium+PhantomJS爬取带Ajax、Js的网页

    Python3.x:Selenium+PhantomJS爬取带Ajax.Js的网页 前言 现在很多网站的都大量使用JavaScript,或者使用了Ajax技术.这样在网页加载完成后,url虽然不改变但 ...

  8. 利用linux curl爬取网站数据

    看到一个看球网站的以下截图红色框数据,想爬取下来,通常爬取网站数据一般都会从java或者python爬取,但本人这两个都不会,只会shell脚本,于是硬着头皮试一下用shell爬取,方法很笨重,但旨在 ...

  9. selenium+phantomjs爬取bilibili

    selenium+phantomjs爬取bilibili 首先我们要下载phantomjs 你可以到 http://phantomjs.org/download.html 这里去下载 下载完之后解压到 ...

  10. scrapy框架之CrawlSpider全站自动爬取

    全站数据爬取的方式 1.通过递归的方式进行深度和广度爬取全站数据,可参考相关博文(全站图片爬取),手动借助scrapy.Request模块发起请求. 2.对于一定规则网站的全站数据爬取,可以使用Cra ...

随机推荐

  1. QT使用中出现的问题

    1.运行程序程序弹出The CDB process terminated 2.调试弹出窗口提示缺少qtcreatorcdbext.dll 1.运行程序程序弹出The CDB process termi ...

  2. 读取excel等文件根据注解自动装填为实体类

    问题:以前每次读取excel 都是根据第几列来装填实体类里面的属性.写起来很麻烦.还要判断. 思路: 1.因为每次读取excel 或者word表格 都能得到是第几列的数据,那么可以知道每列数据的ind ...

  3. jemter 分布式压测

    1.测试机搭建 首选 压力机A,压力机B,压力机C, 压力机A作为控制台 压力机B,压力机C作为分布式的测试机 压力机Aip:172.16.23.69, 压力机Bip:192.168.184.128 ...

  4. 808.11ac的MAC层

    MAC层是802.11的主要功能部分.上层应用通过调用MAC层提供的接口原语调用MAC层的功能. 在内部,MAC由除了函数还有数据,叫MIB,存储MAC的各种参数.SME是一个单独的模块,用来跟接口函 ...

  5. TP3.2.x判断手机端访问,同一个域名在PC和手机端展示不同模板(半独立式网站)

    首先介绍APP_STATUS内置常量,TP入口文件增加APP_STATUS 参数,  自动加载不同的项目配置文件,通过配置文件指向不同的模块 手机端访问时调用Wap手机模块,实现在手机端访问时展示出手 ...

  6. FCOS网络(free anchor)

    FCOS FCOS网络解析 FPN输出多个特征图,然后如何处理这些特征图? [问题]"特征图相对原图的步距是s"是个什么东西

  7. K8S-pod详解

    目录: namespace六大类型 Pod基础概念 Pod两种使用方式 通常把Pod分为两类 Pod容器的分类 init的容器作用 镜像拉取策略(image PullPOlicy) 部署harbor创 ...

  8. TS补充笔记

    TS掘金笔记:https://juejin.cn/post/6872111128135073806    *为疑惑点 类型总结: 2.6.1Enum类型数字枚举设置初始值: 2.6.1.1给第一个枚举 ...

  9. [转]cfs 调度

    https://www.cnblogs.com/LoyenWang/p/12495319.html 背景 Read the fucking source code! --By 鲁迅 A picture ...

  10. 并发多线程学习(六)Java线程间的通信

    合理的使用Java多线程可以更好地利用服务器资源.一般来讲,线程内部有自己私有的线程上下文,互不干扰.但是当我们需要多个线程之间相互协作的时候,就需要我们掌握Java线程的通信方式.本文将介绍Java ...