赞
踩
目录
- <dependency>
- <groupId>org.seleniumhq.selenium</groupId>
- <artifactId>selenium-java</artifactId>
- <version>3.141.59</version>
- </dependency>
- package com.jmh.utils;
-
- import org.openqa.selenium.By;
- import org.openqa.selenium.JavascriptExecutor;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
-
- import java.io.*;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.UUID;
-
- /**
- * 爬取猫眼毛榜单的所有图片并下载到电脑本地
- */
- public class Demo03 {
- //定义一个driver
- private static WebDriver driver;
- //定义一个集合来保存图片
- private static List<String> urlImg = new ArrayList<>();
-
- //定义静态代码块
- static {
- //设置驱动
- System.setProperty("webdriver.chrome.driver","D:\\webDriver\\chromedriver_win32\\chromedriver.exe");
- //创建驱动
- driver=new ChromeDriver();
- }
- //定义一个爬取猫眼榜单图片的方法
- public static void getUrlImg() throws Exception{
- //定义要爬取的网站
- driver.get("https://www.maoyan.com/");
- //设置休眠时间
- Thread.sleep(1000l);
- //定位到榜单
- WebElement element = driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/div[5]/a"));
- //点击榜单
- element.click();
- //设置休眠时间
- Thread.sleep(1000l);
- //执行js,滚动条下拉到最底
- ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
- //设置休眠时间
- Thread.sleep(5000l);
- //定位每个榜单的作品
- //*[@id="app"]/div/div/div/dl/dd[1]
- List<WebElement> elements = driver.findElements(By.xpath("//*[@id=\"app\"]/div/div/div/dl/dd"));
- //遍历每一个作品
- for (WebElement el : elements) {
- /*System.out.println(el.getText());*/
- WebElement el1 = el.findElement(By.className("name"));
- WebElement el2 = el.findElement(By.className("star"));
- WebElement el3 = el.findElement(By.className("releasetime"));
-
- //获取图片
- WebElement element1 = el.findElement(By.cssSelector("img.board-img"));
- String src = element1.getAttribute("src");
- System.out.println(el1.getText());
- System.out.println(el2.getText());
- System.out.println(el3.getText());
- System.out.println(src);
- //将图片添加到集合里面
- urlImg.add(src);
- }
-
- }
- //定义一个将集合里面的图片路径写入到本地电脑里面的方法
- public static void setUrlImg(){
- try {
- //定义网络字节流的通道
- URL url=null;
- //定义图片保存到本地路径
- String path="D:\\img\\";
- //遍历集合
- for (String s : urlImg) {
- /*Thread.sleep(1000l);*/
- System.out.println("集合里面的值"+s);
- url=new URL(s);
- //创建输入流
- InputStream is=new DataInputStream(url.openStream());
- String fileName=path+ UUID.randomUUID().toString().replace("-","")+".jpg";
- //创建输出流
- OutputStream out=new FileOutputStream(new File(fileName));
- byte[] bytes=new byte[1024];
- int len=0;
- while((len=is.read(bytes))!=-1){
- out.write(bytes,0,len);
- }
- is.close();
- out.close();
- }
-
- }catch (Exception e){
-
- }
- }
- public static void main(String[] args) throws Exception{
- //调用获取图片的方法
- getUrlImg();
- //调用将图片保存到电脑本地的方法
- setUrlImg();
-
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。