在之前的文章中,我向您介绍了两种使用Java进行Web抓取的工具。 第一篇文章中的HtmlUnit和文章中有关处理Javascript繁重网站的PhantomJS 。
这次,我们将介绍Chrome的一项新功能,即无头模式。 有传言称,谷歌使用特殊版本的Chrome来满足其抓取需求。 我不知道这是否成立,但是几个月前Google推出了Chrome 59的无头模式。
PhantomJS是该领域的领导者,它一直(并且仍然)大量用于浏览器自动化和测试。 听到有关Headless Chrome的消息后,PhantomJS维护者说他辞去维护者的职务,因为我引述“ Google Chrome比PhantomJS更快,更稳定[...]”
在浏览器自动化和处理Java繁重的网站方面,Chrome无头之徒似乎正成为一种选择。
HtmlUnit,PhantomJS和其他无头浏览器是非常有用的工具,问题在于它们不如Chrome稳定,有时您会遇到Chrome不会发生的Javascript错误。
先决条件
- 谷歌浏览器> 59
- Chromedriver
- 硒
- 在您的pom.xml中添加Selenium的最新版本:
<dependency>
<groupId> org.seleniumhq.selenium </groupId>
<artifactId> selenium-java </artifactId>
<version> 3.8.1 </version>
</dependency>
如果您尚未安装Google Chrome浏览器,则可以在此处下载
要安装Chromedriver,您可以在MacOS上使用brew:
brew install chromedriver
或使用下面的链接下载。
有很多版本,建议您使用最新版本的Chrome和chromedriver。
让我们登录黑客新闻
在这一部分中,我们将登录到Hacker News,并在登录后获取屏幕截图。我们不需要Chrome无头即可完成此任务,但是本文的目的只是向您展示如何使用Selenium运行无头Chrome 。
我们要做的第一件事是创建一个WebDriver对象,并设置chromedriver路径和一些参数:
// Init chromedriver
String chromeDriverPath = "/Path/To/Chromedriver" ;
System . setProperty ( "webdriver.chrome.driver" , chromeDriverPath );
ChromeOptions options = new ChromeOptions ();
options . addArguments ( "--headless" , "--disable-gpu" , "--window-size=1920,1200" , "--ignore-certificate-errors" );
WebDriver driver = new ChromeDriver ( options );
的
option is needed on Windows systems, according to the [documentation](https://developers.google.com/web/updates/2017/04/headless-chrome)
Chromedriver should automatically find the Google Chrome executable path, if you have a special installation, or if you want to use a different version of Chrome, you can do it with :
```java
options.setBinary("/Path/to/specific/version/of/Google Chrome");
如果您想了解更多有关不同选项的信息,请参阅Chromedriver文档
下一步是对Hacker News登录表单执行GET请求,选择用户名和密码字段,并在其中填写我们的凭据,然后单击登录按钮。 然后,我们必须检查凭据错误,如果登录,我们可以截屏。
我们已经在上一篇文章中做到了,这是完整的代码:
public class ChromeHeadlessTest {
private static String userName = "" ;
private static String password = "" ;
public static void main ( String [] args ) throws IOException {
String chromeDriverPath = "/your/chromedriver/path" ;
System . setProperty ( "webdriver.chrome.driver" , chromeDriverPath );
ChromeOptions options = new ChromeOptions ();
options . addArguments ( "--headless" , "--disable-gpu" , "--window-size=1920,1200" , "--ignore-certificate-errors" , "--silent" );
WebDriver driver = new ChromeDriver ( options );
// Get the login page
driver . get ( "https://news.ycombinator.com/login?goto=news" );
// Search for username / password input and fill the inputs
driver . findElement ( By . xpath ( "//input[@name='acct']" )). sendKeys ( userName );
driver . findElement ( By . xpath ( "//input[@type='password']" )). sendKeys ( password );
// Locate the login button and click on it
driver . findElement ( By . xpath ( "//input[@value='login']" )). click ();
if ( driver . getCurrentUrl (). equals ( "https://news.ycombinator.com/login" )){
System . out . println ( "Incorrect credentials" );
driver . quit ();
System . exit ( 1 );
} else {
System . out . println ( "Successfuly logged in" );
}
// Take a screenshot of the current page
File screenshot = (( TakesScreenshot ) driver ). getScreenshotAs ( OutputType . FILE );
FileUtils . copyFile ( screenshot , new File ( "screenshot.png" ));
// Logout
driver . findElement ( By . id ( "logout" )). click ();
driver . quit ();
}
}
在通过身份验证时,您现在应该拥有Hacker News主页的漂亮屏幕截图。 如您所见,Chrome无头确实非常易于使用,它与PhantomJS并没有什么不同,因为我们使用Selenium来运行它。
如果您喜欢此消息,请立即订阅我们的新闻通讯!
如果您喜欢网页抓取并厌倦了代理,JS渲染和验证码的处理,则可以查看我们新的网页抓取API ,其中有前1000个API调用。
和往常一样,该代码可在此Github存储库中找到
From: https://dev.to/scrapingbee/introduction-to-chrome-headless-469b