目的:本文主要描述如何使用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. wampServer本地php环境配置笔记

    红色的为关键步骤,其余可以不看. 一:安装时 , 报 : 1.应用程序无法正常启动0xc0000007b 清单机确定关闭应用程序 : 2. 缺少XXX.dll文件 解决方法都是: C:\Windows ...

  2. NXOpen获取UFUN的tag

    #include <NXOpen/NXObject.hxx>#include <NXOpen/NXObjectManager.hxx> 1 NXObject* nXObject ...

  3. 初学银河麒麟linux笔记 第七章 VMWare虚拟机内的qt程序连接串口和网口

    QT程序可以正常在虚拟机里的麒麟系统里运行了,但是无法连接网口和串口,这里进行配置 网口连接 与硬件的TCP连接,在虚拟机上设置桥接 再重启就能连上了 串口连接 首先参考 https://blog.c ...

  4. 快速确定execl 列数

    1.在最后的列输入公式=COLUMN(). 2.按回车

  5. 推荐一个 python学习网站

    kaggle python课程: https://www.kaggle.com/learn/python 知乎有个博主在专栏放了课程的中文版: https://www.zhihu.com/people ...

  6. Ubuntu截图软件

    Ubuntu截图软件 方法一:使用系统自带的快捷键 可以将其修改为自己习惯的快捷键 如图: 方式二:使用软件ksnip GitHub: https://github.com/ksnip/ksnip 安 ...

  7. 使用viper读取配置文件

    配置文件config.yml mysql: type: mysql dsn: "user:pass@tcp(localhost:30306)/db_name?charset=utf8& ...

  8. 动态构造LINQ表达式导致EFCore内存泄漏

    EFCore版本 v3.1.4 上述代码模拟100次的Id包含查询,并且demoExpr1和demoExpr2使用两种方式构造LINQ表达式,第二种会导致内存泄漏. 使用第一种方法构造查询条件的值,结 ...

  9. 大小写字符转换【Sql Server和C#两种写法】

    案例:Var Str = "abdCnd" 如何将Str = "ABDCND"? Sql Server写法:upper(Str)   ==>  Lower ...

  10. source insight c++ namespace 无法跳转解决方法

    source insight c++ namespace 无法跳转解决方法 2016年02月15日 11:47:35 暗小夜 阅读数:4460   勾选igore namespace declarat ...