目录
今天与大家分享一个小案例 简单来说就是通过Selenium爬取京东/html页面的商品信息、图片...
一、实例 指定网站 下载到本地的图片
效果展示
下载到本地图片
最后的通过Selenium下载到本地的代码
package demo; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.UUID; public class Demo03 { public static void main(String[] args) throws IOException { //设置驱动 System.setProperty("webdriver.chrome.driver", "D:\\temp\\chromedriver_win32 (1)\\chromedriver.exe"); //创建驱动 ChromeDriver driver = new ChromeDriver(); //建立连接路径 driver.get("https://m.gmw.cn/baijia/2022-06/30/1303021712.html"); //获取图片集合 List<WebElement> elements = driver.findElements(By.cssSelector(".contentImage img")); List<String> images = new ArrayList<>(); for (WebElement e : elements) { String img = e.getAttribute("src"); System.out.println(img); images.add(img); sleep(3000); } for (String m : images) { URL url = new URL(m); InputStream in = url.openStream(); String path = "D:\\temp\\" + UUID.randomUUID().toString().replace("-", "") + ".jpg"; FileOutputStream out = new FileOutputStream(path); byte[] tmp = new byte[1024]; int len = 0; while ((len = in.read(tmp)) != -1) { out.write(tmp, 0, len); } out.flush(); out.close(); in.close(); sleep(3000); } } private static void sleep(int n) { try { Thread.sleep(n); } catch (InterruptedException e) { e.printStackTrace(); } } }