我们公司的需求总是那么折磨人,最近要做模拟人一步步点击拼多多商品发起支付然后得到最后一步的链接。那么之前学的selenium就派上用场了,在本篇博客我将带大家学会Java爬虫的第一步,用selenium模拟人点击一个个的标签,得到我们想要的网页或者链接。

一、驱动IE浏览器

1.1 准备工作 下载ie浏览器驱动,IEDriverServer的版本号和Selenium的版本号一定要一致,然后我把IEDriverServer.exe充命名Quark.exe放在本地C:\Program Files下面(也可以写个方法把Quark.exe放在项目里启动项目时把它拷贝到C:\Program Files下面)。

    public static boolean doPrepare(String path) {boolean flag = false;ClassPathResource classPathResource = new ClassPathResource("tool/QuarkGG.exe");String filename = classPathResource.getFilename();try {InputStream inputStream = classPathResource.getInputStream();byte[] bytes = FileCopyUtils.copyToByteArray(inputStream);String fileName = new String(filename.getBytes("UTF-8"), "iso-8859-1");String filePath = path + File.separator + fileName;File file = new File(filePath);if (!file.exists()){FileOutputStream output = new FileOutputStream(file);output.write(bytes);}flag = true;} catch (IOException e){e.printStackTrace();}return flag;}

1.2 创建Springboot项目

1.2.1添加maven依赖

  <dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-ie-driver</artifactId><version>3.141.59</version></dependency>

1.2.2写核心类

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;@RestController
@Slf4j
public class OrderController {private WebDriver driver;private static String path = "C:\\Program Files";private boolean ifonce = true;@GetMapping("/order")public String openAndLogin() throws InterruptedException {//找到本地ie驱动if (StringUtils.isEmpty(path)) {System.out.println("没有找到Quark");return null;}//设置驱动位置属性System.setProperty("webdriver.ie.driver", path + File.separator + "Quark.exe");driver = new InternetExplorerDriver();//        System.setProperty("webdriver.chrome.driver", path + File.separator + "QuarkGG.exe");
//        driver = new ChromeDriver();driver.manage().window().maximize();driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);if (driver == null) {return null;}//TODO 模拟手点driver.get("https://mobile.yangkeduo.com/goods2.html?goods_id=7419459408&page_from=23&share_uin=S7GZQW52Z3VAWAYRWXMUOOWTI4_GEXDA&refer_share_id=d04900312af648d38e3d3cc3a49f0f1f&refer_share_uid=4480713700&refer_share_channel=");//TODO 窗口切换
//        if(ifonce) {//            System.out.println("URL1:" + driver.getCurrentUrl());
//            String url1=driver.getCurrentUrl();
//            Set winHandels = driver.getWindowHandles();
//            List it = new ArrayList(winHandels);
//            driver.switchTo().window((String) it.get(0));
//            Thread.sleep(1000);
//            if(driver.getCurrentUrl().equals(url1)){//                driver.switchTo().window((String) it.get(1));
//            }
//            System.out.println("URL2:" + driver.getCurrentUrl());
//            ifonce=false;
//        }Thread.sleep(500);//找到购买标签,点击driver.findElement(By.className("goods-buy-price")).click();Thread.sleep(1000);//选择商品属性,点击List<WebElement> list = driver.findElements(By.className("sku-spec-value"));for (WebElement element : list) {Thread.sleep(500);element.click();}Thread.sleep(1000);//点击购买,点击driver.findElement(By.className("sku-selector-bottom")).click();Thread.sleep(1000);//得到购买链接String url = driver.getCurrentUrl();log.info(url);return url;}
}

1.3 测试,启动项目浏览器地址栏输入:localhost:8080/order

最后得到支付链接, 搞定。

二、驱动谷歌浏览器

2.1下载驱动,本地谷歌浏览器和驱动的版本匹配很苛刻,根据下面图片来,我是讲下载好的ChromeSetup.exe重命名QuarkGG,然后放C:\Program Files下面。

2.2.1 在Springboot项目中添加谷歌驱动依赖

<dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-chrome-driver</artifactId><version>3.141.59</version></dependency>

2.2.2 把核心类的驱动换到谷歌驱动

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;@RestController
@Slf4j
public class OrderController {private WebDriver driver;private static String path = "C:\\Program Files";private boolean ifonce = true;@GetMapping("/order")public String openAndLogin() throws InterruptedException {//找到本地ie驱动if (StringUtils.isEmpty(path)) {System.out.println("没有找到Quark");return null;}//设置驱动位置属性
//        System.setProperty("webdriver.ie.driver", path + File.separator + "Quark.exe");
//        driver = new InternetExplorerDriver();System.setProperty("webdriver.chrome.driver", path + File.separator + "QuarkGG.exe");driver = new ChromeDriver();driver.manage().window().maximize();driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);if (driver == null) {return null;}//TODO 模拟手点driver.get("https://mobile.yangkeduo.com/goods2.html?goods_id=7419459408&page_from=23&share_uin=S7GZQW52Z3VAWAYRWXMUOOWTI4_GEXDA&refer_share_id=d04900312af648d38e3d3cc3a49f0f1f&refer_share_uid=4480713700&refer_share_channel=");//TODO 窗口切换
//        if(ifonce) {//            System.out.println("URL1:" + driver.getCurrentUrl());
//            String url1=driver.getCurrentUrl();
//            Set winHandels = driver.getWindowHandles();
//            List it = new ArrayList(winHandels);
//            driver.switchTo().window((String) it.get(0));
//            Thread.sleep(1000);
//            if(driver.getCurrentUrl().equals(url1)){//                driver.switchTo().window((String) it.get(1));
//            }
//            System.out.println("URL2:" + driver.getCurrentUrl());
//            ifonce=false;
//        }Thread.sleep(500);//找到购买标签,点击driver.findElement(By.className("goods-buy-price")).click();Thread.sleep(1000);//选择商品属性,点击List<WebElement> list = driver.findElements(By.className("sku-spec-value"));for (WebElement element : list) {Thread.sleep(500);element.click();}Thread.sleep(1000);//点击购买,点击driver.findElement(By.className("sku-selector-bottom")).click();Thread.sleep(1000);//得到购买链接String url = driver.getCurrentUrl();log.info(url);return url;}
}

2.3 测试,启动项目浏览器地址栏输入:localhost:8080/order

测试成功。

java爬虫(一)用selenium驱动IE和谷歌浏览器模拟点击网页相关推荐

  1. java怎样模拟点击网页,笔者操作Java+selenium实现网站模拟点击和页面数据爬取

    电脑现已成为我们工作.生活和娱乐必不可少的工具了,在使用电脑的过程中,可能会遇到Java+selenium实现网站模拟点击和页面数据爬取的问题,如果我们遇到了Java+selenium实现网站模拟点击 ...

  2. 伪解决Selenium中调用PhantomJS无法模拟点击(click)操作

    Python 2.7 IDE Pycharm 5.0.3 具体Selenium和PhantomJS配置及使用请看调用PhantomJS.exe自动续借图书馆书籍 我一直以为,PhantomJS就是无界 ...

  3. 【Python】Selenium模拟点击网页下载文件

    整个流程大致如下: 1.首先需要在http://chromedriver.storage.googleapis.com/index.html中下载chrome浏览器版本对应的驱动文件,可以在浏览器[设 ...

  4. python 自动点击上传以后上传文件,python使用selenium模拟点击网页实现自动导入上传文件功能...

    一.环境准备 Python版本:3.4 编辑器:Pycharm excel文件:导入的excel模板 二.python代码 由于工作需要,需要每天定时导入相关excel文件进入后台数据库,由于导入的逻 ...

  5. 爬虫学习笔记——Selenium爬取淘宝商品信息并保存

    在使用selenium来模拟浏览器操作,抓取淘宝商品信息前,先完成一些准备工作. 准备工作:需要安装selenium,pyquery,以及Chrome浏览器并配置ChromeDriver. 安装sel ...

  6. Java爬虫进阶-Selenium+PhantomJs的运用

    selenium Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Mozil ...

  7. python 模拟浏览器selenium 微信_Spider-Python爬虫之使用Selenium模拟浏览器行为

    分析 他的代码比较简单,主要有以下的步骤:使用BeautifulSoup库,打开百度贴吧的首页地址,再解析得到id为new_list标签底下的img标签,最后将img标签的图片保存下来. header ...

  8. python爬虫之初恋 selenium

    selenium 是一个web应用测试工具,能够真正的模拟人去操作浏览器. 用她来爬数据比较直观,灵活,和传统的爬虫不同的是, 她真的是打开浏览器,输入表单,点击按钮,模拟登陆,获得数据,样样行.完全 ...

  9. Python爬虫利器五Selenium用法

    在上一节我们学习了 PhantomJS 的基本用法,归根结底它是一个没有界面的浏览器,而且运行的是 JavaScript 脚本,然而这就能写爬虫了吗?这又和 Python 有什么关系?说好的 Pyth ...

最新文章

  1. 大数据产品不仅仅是IT工具
  2. ARM的批量加载/存储指令
  3. #17# SCCM管理 - 软件中心 VS 应用程序目录网站点
  4. EAGER的获取是代码的味道
  5. SharePoint 2013 关于自定义显示列表表单的bug
  6. 【Python】从键盘输入一个大于1的整数N,判断是否为素数
  7. Python可视化:Seaborn(三)
  8. 解决Visual C++ for Linux: -L~/projects/path_to_lib_folder 无法设置library search path的问题...
  9. TVS ESD 二极管介绍与应用
  10. 《C语言入门经典》读后感(一)
  11. 理解辐射校正、辐射定标、大气校正关系
  12. Swin-Transformer-Object-Detection V2.11.0环境搭建(一)
  13. 单片机实验13:用热敏电阻和ADC实现测量温度
  14. 认识IL代码---从开始到现在 第二篇
  15. 删除后别人的微信号变成wxid_“ 微信号 ” 和 “ 微信账号 ” 分别代表什么?...
  16. win10安装过程中一直卡在海内存知己天涯若比邻界面解决方法
  17. Android - 分屏模式(多窗口模式)
  18. Emmagee—开源Android性能测试工具
  19. hdf5 matlab,hdf5格式的matlab读写操作
  20. 小把戏之——用电脑或手机打出拼音带音标!

热门文章

  1. 一个好的产品是如何做好兼容性测试的?
  2. 企业案例丨腾讯广告助手 X 云开发CloudBase
  3. 工作中,我们应该走捷径吗?
  4. Linux基础命令集合
  5. 再也不能迟到了!单位启用人脸识别考勤
  6. windows笔记本下ubuntu双系统安装
  7. vc6编译出小体积pe文件
  8. shell脚本 日期转时间戳
  9. LSTM模型预测时间序列性质的进件量---详细步骤以及例子
  10. Android四大组件:广播机制——BroadcastReceiver