看很多大佬都在用python爬取图片,其实java也可以啦,这里就通过一个例子来跑一把啦
话不多说,上代码
private final static String driver = “webdriver.chrome.driver”;
private final static String chromeDriver = “C:\Users\d\AppData\Local\Google\Chrome\Application\chromedriver.exe”;
//网址
private final static String emojiWebAddress = "https://www.dbbqb.com/";
static {
// 引入谷歌驱动 控制浏览器
System.setProperty(driver, chromeDriver);
}
public static void main(String[] args) {
System.out.println("正在打开浏览器");
//1、获取谷歌浏览器控制 打开浏览器
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();//浏览器最大化
//2、超时等待30秒
Duration duration = Duration.ofSeconds(3);
driver.manage().timeouts().implicitlyWait(duration);
//3、跳转到表情包网址
driver.get(emojiWebAddress);
while (true){
driver.navigate().refresh();
try {
//4、获取到元素
WebElement bqbEle = driver.findElement(By.xpath("/
private static void downloadPicture(String urlList, String path) {
URL url = null;
try {
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我运行后,发现这个网址是有反爬机制的,这里用了一种无限循环的方式来解决,反正每次刷新图都不一样,代码仅供学习嗷,这里建议大家千万别去恶意爬取人家的资源,容易被逮。
文章转自:selenium+java爬取图片_Java-答学网
作者:答学网,转载请注明原文链接:http://www.dxzl8.com/