WebMagic虽然使用方便,但奈何针对性太强,而且模拟请求的方法很容易被发现以及被反爬,所以在多次因网站更新而被迫跟新爬虫的教训下,决定使用selenium来模拟点击获取内容。
这是依赖:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
因为本身selenium就是用于自动化测试的,更像是一个脚本,所以相比于WebMagic更灵活,也更不容易被反爬
public class BaiduTest {
public static void main(String[] args) {
String url = "https://index.baidu.com/v2/index.html#/";
runTest(url);
}
private static void runTest(String url) {
//需要下载浏览器驱动,来模拟浏览器,这里用的是火狐,下载地址:https://github.com/mozilla/geckodriver/releases
System.setProperty("webdriver.gecko.driver", "D:\\workspace\\MyWorkTools\\geckodriver.exe");
//创建驱动器
FirefoxDriver driver =