Selenium用法详解【Options选项】【JAVA爬虫】

简介

本文主要讲解如何使用java代码利用selenium控制浏览器的启动选项Options的代码操作教程。

Options选项

这是一个Chrome的参数对象,在此对象中使用addArgument()方法可以添加启动参数,添加完毕后可以在初始化Webdriver对象时将此Options对象传入,则可以实现以特定参数启动Chrome。

设置浏览器后台运行

后台运行浏览器,通过selenium取到,洛阳泰山博客的,博主名字。

代码如下:


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.chrome.ChromeOptions;


/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";

    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        // 设置后台静默模式启动浏览器
        chromeOptions.addArguments("--headless");
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://blog.csdn.net/weixin_40986713");
        WebElement element= driver.findElement(By.xpath("//div[@class='user-profile-head-name']/div[1]"));
         //输出元素里的文本内容
        System.out.println(element.getText());
    }
}

代码中还提供了另一种后台运行的方法,和上面的效果一样。

 // 设置后台静默模式启动浏览器
   chromeOptions.setHeadless(true);

设置浏览器最大化


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";

    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        //设置浏览器启动最大化(windows写法)
        chromeOptions.addArguments("start-maximized");
         //mac写法
        //chromeOptions.addArguments("--start-fullscreen");
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://blog.csdn.net/weixin_40986713");
    }
}

自定义浏览器大小

     //自定义浏览器窗口大小
        chromeOptions.addArguments("--window-size=1366,768");

加载用户配置

我们在登录网站的时候,通常需要输入用户名、密码和验证码,那么有没有办法绕过登录环节呢?

有两种方法可以解决这个问题,一种是利用cookie,一种是利用chrome浏览器的用户配置,这里主要讲下利用chrome浏览器的用户配置的实现。


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";

    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        //加载用户配置
        chromeOptions.addArguments("--user-data-dir=C:\\Users\\Lenovo\\AppData\\Local\\Google\\Chrome\\User Data");
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://blog.csdn.net/weixin_40986713");
    }
}

如何查看User Data的路径,chrome浏览器里输入chrome://version,即可查看User Data的路径

隐藏指纹特征

selenium 对于部分网站来说十分强大,但它也不是万能的,实际上,selenium 启动的浏览器,有几十个特征可以被网站检测到,轻松的识别出你是爬虫。

不相信?接着往下看,首先你手动打开浏览器输入https://bot.sannysoft.com/,在网络无异常的情况下,显示应该如下:

下面通过 selenium 来打开浏览器。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";

    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        WebDriver driver = new ChromeDriver();
        driver.get("https://bot.sannysoft.com/");
    }
}

通过 webdriver:present 可以看到浏览器已经识别出了你是爬虫,我们再试一下无头浏览器。


import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;


/**
 * @author tarzan
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";

    public static void main(String[] args) throws InterruptedException, IOException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        //后台运行
        chromeOptions.setHeadless(true);
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://bot.sannysoft.com/");
        Thread.sleep(1000);
        // 截图操作
        File sourceFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        // 截图存储
        FileUtils.copyFile(sourceFile, new File("E:\\screenshot\\"+driver.getTitle()+".png"));
    }
}

没错,就是这么真实,对于常规网站可能没什么反爬,但真正想要抓你还是一抓一个准的。

说了这么多,是不是 selenium 真的不行?别着急,实际还是解决方法的。关键点在于如何在浏览器检测之前将这些特征进行隐藏,事实上,前人已经为我们铺好了路,解决这个问题的关键,只需要配置chromeOptions设置特征隐藏就行。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";

    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        //禁用WebDriver特征
        chromeOptions.addArguments("--disable-blink-features");
        chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
        //隐身模式
        chromeOptions.addArguments("--incognito");
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://bot.sannysoft.com/");
    }
}

隐身模式 意思是 Web 浏览器上的一个设置,允许您在浏览互联网时隐藏起来。隐身模式的工作原理是从 Web 浏览会话中删除本地数据。这意味着您的本地搜索历史记录中不会记录任何浏览;网站试图上传到您计算机的任何 cookie 都将被删除或阻止。其他跟踪程序、临时文件和第三方工具栏也被禁用。

禁用浏览器正在被自动化程序控制的提示

//chrome 76版本以前的写法
chromeOptions.AddArgument("disable-infobars");
//chrome 76版本以后的写法
 chromeOptions.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});

模拟移动设备

//模拟iPhone 6
chromeOptions.addArguments("user-agent=\"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
 //模拟 android QQ浏览器
chromeOptions.addArguments("user-agent=\"MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");

添加代理

这个地方尤其需要注意的是,在选择代理时,尽量选择静态IP,才能提升爬取的稳定性。因为如果选择selenium来做爬虫,说明网站的反爬能力比较高(要不然直接上scrapy了),对网页之间的连贯性,cookies,用户状态等有较高的监测。如果使用动态匿名IP,每个IP的存活时间是很短的(1~3分钟)


import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";

    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        Proxy proxy=new Proxy();
        //示例
        proxy.setHttpProxy("http://D37EPSERV96VT4W2:[email protected]:9020");
        chromeOptions.setProxy(proxy)
        WebDriver driver = new ChromeDriver(chromeOptions);
    }
}

设置chrome的下载路径


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;



/**
 * @author Lenovo
 */
public class SeleniumDemo {
    private final static String webDriverName = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";

    public static void main(String[] args) throws InterruptedException {
        System.setProperty(webDriverName, webDriverPath);
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("download.default_directory", "D://download");
        WebDriver driver = new ChromeDriver(chromeOptions);
    }
}

设置编码格式

        //设置浏览器编码为UTF-8
        chromeOptions.addArguments("lang=zh_CN.UTF-8");

猜你喜欢

转载自blog.csdn.net/weixin_40986713/article/details/128612616
今日推荐