import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import top.lvzhiqiang.entity.VideoInfoPool; import top.lvzhiqiang.util.DateUtils; import java.io.*; import java.net.InetSocketAddress; import java.net.Proxy; import java.time.LocalDate; import java.time.LocalDateTime; public class Test2 { public static void main(String[] args) { String javbusUrl = "https://www.seejav.work/"; String identificationCode = "MGMQ-053";// DTT-049 HAWA-243 HISN-011 DANDY-745 VOSS-172 PFES-024 VIDA-005 SHKD-843 CAWD-176 BLK-467 Document document; VideoInfoPool videoInfoPool; try { document = Jsoup.connect(javbusUrl.concat(identificationCode)).timeout(50000).get(); videoInfoPool = new VideoInfoPool(); parseDocument(document, identificationCode, videoInfoPool); System.out.println(videoInfoPool); } catch (Exception e) { e.printStackTrace(); } } private static void parseDocument(Document document, String identificationCode, VideoInfoPool videoInfoPool) throws Exception { Elements container = document.select("div.container"); if (container.size() == 0) { throw new Exception("番号无效!"); } // 名称 String h3 = container.select("h3").first().text(); String[] nameArr = h3.split("\\s+"); if (nameArr.length > 1) { videoInfoPool.setName(h3.substring(nameArr[0].length()).trim()); } else { videoInfoPool.setName(nameArr[0]); } Elements pEles = container.select("div.info > p"); // 识别码 Element pEle = pEles.get(0); String iCode = pEle.select("span[style]").first().text(); if (!identificationCode.equalsIgnoreCase(iCode)) { throw new Exception("番号与站点不一致"); } videoInfoPool.setIdentificationCode(iCode); // 发行日期 pEle = pEles.get(1); String issueDate = pEle.text().split(":")[1].replace("\"", "").trim(); videoInfoPool.setIssueDate(LocalDate.parse(issueDate, DateUtils.dateFormatter)); // 长度 pEle = pEles.get(2); String length = pEle.text().split(":")[1].replace("\"", "").trim(); videoInfoPool.setLength(length); // 导演 Elements directorEles = container.select("div.info").select("p:contains(導演)"); if (directorEles.size() > 0) { pEle = directorEles.first().select("a[href]").first(); videoInfoPool.setDirector(pEle.text()); } // 制作商 Elements markerEles = container.select("div.info").select("p:contains(製作商)"); if (markerEles.size() > 0) { pEle = markerEles.first().select("a[href]").first(); videoInfoPool.setMaker(pEle.text()); } // 发行商 Elements issuerEles = container.select("div.info").select("p:contains(發行商)"); if (issuerEles.size() > 0) { pEle = issuerEles.first().select("a[href]").first(); videoInfoPool.setIssuer(pEle.text()); } // 类别 Elements genresEles = container.select("div.info").select("p:contains(類別)"); if (genresEles.size() > 0) { StringBuffer sb = new StringBuffer(); Elements ahrefEles = genresEles.first().nextElementSibling().select("a[href]"); for (Element ahrefEle : ahrefEles) { sb.append(ahrefEle.text()).append(","); } if (sb.length() > 0) { sb = sb.deleteCharAt(sb.length() - 1); } videoInfoPool.setGenres(sb.toString()); } // 演员 Elements castEles = container.select("div.info").select("p.star-show:contains(演員)"); if (castEles.size() > 0) { Elements castElesTemp = container.select("div.info:contains(暫無出演者資訊)"); if (castElesTemp.size() == 0) { StringBuffer sb = new StringBuffer(); Elements ahrefEles = castEles.first().nextElementSibling().nextElementSibling().select("a[href]"); for (Element ahrefEle : ahrefEles) { sb.append(ahrefEle.text()).append(","); } if (sb.length() > 0) { sb = sb.deleteCharAt(sb.length() - 1); } videoInfoPool.setCast(sb.toString()); } } // 图片URL String href = container.select("a.bigImage").first().attr("abs:href"); Connection.Response response = Jsoup.connect(href).method(Connection.Method.GET).ignoreContentType(true).timeout(50 * 1000).execute(); String machiPath = "F:/1/0/2/4/视频/电影/A级(成人级)/码池/"; String fileName = issueDate.concat(" ").concat(h3).concat(".jpg"); //saveFile(response.bodyStream(), machiPath.concat(fileName)); videoInfoPool.setImgUrl(fileName); videoInfoPool.setCreateTime(LocalDateTime.now()); } /** * 保存文件到本地 * * @param bufferedInputStream * @param savePath */ private static void saveFile(BufferedInputStream bufferedInputStream, String savePath) throws IOException { //一次最多读取1k byte[] buffer = new byte[1024]; //实际读取的长度 int readLenghth; //创建的一个写出的缓冲流 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(savePath))); //文件逐步写入本地 while ((readLenghth = bufferedInputStream.read(buffer, 0, 1024)) != -1) {//先读出来,保存在buffer数组中 bufferedOutputStream.write(buffer, 0, readLenghth);//再从buffer中取出来保存到本地 } //关闭缓冲流 bufferedOutputStream.close(); bufferedInputStream.close(); } }