Java+selenium自动爬取网站内容并写入本地
目的:本文主要描述如何使用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自动爬取网站内容并写入本地的更多相关文章
- selenium自动爬取网易易盾的验证码
我们在爬虫过程中难免会遇到一些拦路虎,比如各种各样的验证码,时不时蹦出来,这时候我们需要去识别它来继续我们的工作,接下来我将爬取网一些滑动验证码,然后通过百度的EasyDL平台进行数据标注,创建模型, ...
- 利用Jsoup包爬取网站内容
一 Jsoup包 下载链接:http://download.csdn.net/detail/u014000832/7994245 二 爬取搜狐新闻网站标题等内容 package com.test1; ...
- 用selenium 自动爬取某一本小说章节及其内容,并存入数据库中
from selenium import webdriver import pymysql from selenium.webdriver.support.ui import WebDriverWai ...
- python爬取网站视频保存到本地
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: Woo_home PS:如有需要Python学习资料的小伙伴可以加点 ...
- Java爬虫实践--爬取CSDN网站图片为例
实现的效果,自动在工程下创建Pictures文件夹,根据网站URL爬取图片,层层获取.在Pictures下以网站的层级URL命名文件夹,用来装该层URL下的图片.同时将文件名,路径,URL插入数据库, ...
- 使用Selenium爬取网站表格类数据
本文转载自一下网站:Python爬虫(5):Selenium 爬取东方财富网股票财务报表 https://www.makcyun.top/web_scraping_withpython5.html 需 ...
- Python3.x:Selenium+PhantomJS爬取带Ajax、Js的网页
Python3.x:Selenium+PhantomJS爬取带Ajax.Js的网页 前言 现在很多网站的都大量使用JavaScript,或者使用了Ajax技术.这样在网页加载完成后,url虽然不改变但 ...
- 利用linux curl爬取网站数据
看到一个看球网站的以下截图红色框数据,想爬取下来,通常爬取网站数据一般都会从java或者python爬取,但本人这两个都不会,只会shell脚本,于是硬着头皮试一下用shell爬取,方法很笨重,但旨在 ...
- selenium+phantomjs爬取bilibili
selenium+phantomjs爬取bilibili 首先我们要下载phantomjs 你可以到 http://phantomjs.org/download.html 这里去下载 下载完之后解压到 ...
- scrapy框架之CrawlSpider全站自动爬取
全站数据爬取的方式 1.通过递归的方式进行深度和广度爬取全站数据,可参考相关博文(全站图片爬取),手动借助scrapy.Request模块发起请求. 2.对于一定规则网站的全站数据爬取,可以使用Cra ...
随机推荐
- 常用 包vue-clipboard2
包名称 内容 剪切板 vue-clipboard2
- ntpq 详解
Gentoo(也许其他发行版也是?)中 "ntpq -p" 的 man page 只有简短的描述:"打印出该服务器已知的节点列表和它们的状态概要信息." 我还没 ...
- 【DM论文阅读杂记】推荐系统 注意力机制
Paper Title Real-time Attention Based Look-alike Model for Recommender System Basic algorithm and ma ...
- BLOG-2
前言 这几次的PTA作业和考试涉及到的知识点有面向对象中对数据的封装,还有继承和多态,还有抽象类和对象容器 也涉及到了一些,同时还有关于正则表达式的一些内容.而关于题量,这应该对于一个初学者来说是一个 ...
- Excel如何按名字提取另一张表上数据(跨表查询)
1.Excel如何按名字提取另一张表上数据(跨表查询) 公式"=VLOOKUP(A3,[工资信息.xls]Sheet1!$A$3:$B$32,2,0)"原理. 1."A3 ...
- 用C++写的文件字符数、单词数以及总行数的统计(源码)
#include <stdio.h> #include <fstream> #include <string> using namespace std; //计算单 ...
- 【三维重建】Ubuntu20.04进行RealSenseD435环境配置及初步使用
一.环境配置 github上面的教程:https://github.com/IntelRealSense/librealsense/blob/master/doc/distribution_linux ...
- 哲讯科技SAP医疗器械行业ERP解决方案
哲讯科技SAP医疗器械行业ERP解决方案主要体现在以预测为指导,计划为执行的管理理念,完全做到实时的全过程的质量管理和质量跟踪.并且通过灵活的质量管理模块大大降低因实施GMP管理给企业带来的成本压力. ...
- shell 脚本case
#! /bin/bash case $1 in 1) **** ;; 2) **** ;; 3) **** ;; esac
- 树莓派3B 查看GPU、CPU温度
参考:How to find out Raspberry Pi GPU and ARM CPU temperature on Linux GPU温度: /opt/vc/bin/vcgencmd mea ...