作者:lzlrl
java控制鼠标自动点击
package util; import java.awt.AWTException; import java.awt.Color; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; public class 鼠标点击 extends JFrame { public static final SimpleDateFormat sf = new SimpleDateFormat("yyyy_MM_dd HH_mm_ss"); public static Robot robot; public static Dimension screen; public static void init() throws AWTException { screen = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕大小 //System.out.println(screen.width); //System.out.println(screen.height); //Point location = MouseInfo.getPointerInfo().getLocation(); // 获取鼠标当前位置 //System.out.println(location.getX()); //System.out.println(location.getY()); robot = new Robot(); //在处理完当前事件队列只的所有事件之前,一直等待 //robot.waitForIdle(); //设置此robot在生成一个事件后是否自动调用waitForIdle(),设置为true,表示添加的事件逐个按顺序执行 robot.setAutoWaitForIdle(Boolean.TRUE); //设置此robot每在生成一个事件后自动睡眠的毫秒数 //robot.setAutoDelay(2000); //获取指定矩形区域的图像(截图) //BufferedImage bufferedimage=robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); //ImageIcon icon = new ImageIcon(fullScreenImage); robot.delay(3000); } public static void start() { new Thread() { public void run() { //编辑 mouseClick(1157, 354); robot.delay(2000); //编辑连接 for (int i = 0; i < 3; i++) { mouseClick2(1340, 726); } robot.delay(500); Color beginColor1 = new Color(199, 0, 11); Color beginColor2 = robot.getPixelColor(180, 687); if (beginColor1.equals(beginColor2)) { robot.delay(1000); mouseClick(214, 687); robot.delay(5000); } //滚动 // robot.mouseMove(1341, 134); // Color rollColor1 = new Color(238, 240, 245); // Color rollColor2 = robot.getPixelColor(1341, 134); // if (rollColor2.equals(rollColor1)) { // //robot.mouseWheel(100); // } //编辑字段 for (int i = 0; i < 200; i++) { mouseClick2(1340, 726); } robot.delay(500); Color beginColor3 = new Color(199, 0, 11); Color beginColor4 = robot.getPixelColor(270, 687); if (beginColor3.equals(beginColor4)) { robot.delay(3000); mouseClick(307, 687); robot.delay(2000); } else { for (int i = 0; i < 200; i++) { mouseClick2(1340, 726); } if (beginColor3.equals(beginColor4)) { robot.delay(3000); mouseClick(307, 687); robot.delay(2000); } else { robot.delay(3000); mouseClick(307, 687); robot.delay(2000); } } //保存并运行 Color beginColor5 = new Color(199, 0, 11); Color beginColor6 = robot.getPixelColor(350, 530); if (beginColor5.equals(beginColor6)) { mouseClick(395, 530); robot.delay(2000); } //鼠标归位 robot.mouseMove(screen.width / 2, screen.height / 2); } }.start(); } //移动并点击鼠标 public static void mouseClick(int x, int y) { robot.mouseMove(x, y); robot.delay(300); //鼠标左键:BUTTON1_DOWN_MASK 鼠标中键:BUTTON2_DOWN_MASK 鼠标右键:BUTTON3_DOWN_MASK robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK); robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK); } //移动并点击鼠标 public static void mouseClick2(int x, int y) { robot.mouseMove(x, y); //鼠标左键:BUTTON1_DOWN_MASK 鼠标中键:BUTTON2_DOWN_MASK 鼠标右键:BUTTON3_DOWN_MASK robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK); robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK); } public static void main(String[] args) throws Exception { //初始化 init(); int count = 1; while (true) { System.out.print("第 " + (count++) + " 次"); //校准特殊颜色是否正确 不在界面不点击 Color beginColor1 = new Color(199, 0, 11); Color beginColor2 = robot.getPixelColor(90, 227); Color beginColor3 = robot.getPixelColor(90, 257); if (beginColor1.equals(beginColor2) || beginColor1.equals(beginColor3)) { System.out.println("\t开始点击!"); //开始点击 start(); } else { System.out.println("\t跳过!"); } Thread.sleep(5 * 1000); } } }
本文为原创文章,转载请注明出处!
java多线程下载小说网站
aaa
package xiaoshuo; import java.text.DecimalFormat; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; import org.apache.xml.utils.URI; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; import org.jsoup.select.Elements; public class 小说多线程 { static class MyRunnable implements Runnable { private String href; private String title; private String filePath; private String fileName; //原子数 private static AtomicInteger num = new AtomicInteger(); public MyRunnable(String href, String title, String filePath, String fileName) { this.href = href; this.title = title; this.filePath = filePath; this.fileName = fileName; } @Override public void run() { try { //发送请求 String html = 小说Utils.getHttps(href); if (html != null && !"".equals(html)) { //解析内容 Document document = Jsoup.parse(html); Element element = document.getElementById("content"); List<Node> nodes = element.childNodes(); //提取文本 StringBuffer sb = new StringBuffer(); for (Node node : nodes) { String line = ""; if ("#text".equals(node.nodeName())) { line = node.outerHtml(); } else { List<Node> pNode = node.childNodes(); for (Node p : pNode) { line += p.outerHtml(); } } if (!"".equals(line.replace(" ", " ").replaceAll("\s", ""))) { if (!" 天才一秒记住本站地址<br><br> ".equals(line)) { sb.append("\t").append(line.replace(" ", "")).append("\n\n"); } } } String threadName = Thread.currentThread().getName().replace("pool-1-", ""); System.out.println(String.format("%-10s", threadName) + "\t" + num.incrementAndGet() + "\t" + fileName.replace(title, "") + "\t" + title); //写出文件 小说Utils.writeTxt(filePath, fileName, title + "\n\n" + sb.toString()); //延时间隔 Thread.sleep(100); } } catch (Exception e) { e.printStackTrace(); } } } /** 合并文件 */ /*public static void main(String[] args) { String filePath = "C:\\Users\\Administrator\\Desktop\\星空彼岸"; 小说Utils.mergeFile(filePath); }*/ public static void main(String[] args) throws Exception { long startTime = System.currentTimeMillis(); String filePath = "C:\\Users\\Administrator\\Desktop\\星空彼岸"; String url = "http://www.xxx.com/book/25644/"; String html = 小说Utils.getHttps(url); Document document = Jsoup.parse(html); Elements chapter = document.getElementById("list").getElementsByTag("a"); URI uri = new URI(url); String domain = uri.getScheme() + "://" + uri.getHost(); 小说Utils.clearFile(filePath); // 创建线程池 ExecutorService executor = Executors.newFixedThreadPool(100); for (int i = 0; i < chapter.size(); i++) { String href = domain + chapter.get(i).attr("href"); String title = chapter.get(i).html(); //String fileName = arr[0] + "_" + title.replaceAll("[/\\:*?\"<>|]", " "); String fileName = String.format("%04d", i + 1) + "_" + title.replaceAll("[^0-9a-zA-Z\u4e00-\u9fa5]-.()()", " ").trim(); //创建线程 MyRunnable myRunnable = new MyRunnable(href, title, filePath, fileName); //执行线程 executor.execute(myRunnable); Thread.sleep(100); } //执行完毕后停止线程池 executor.shutdown(); //阻塞主线程 判断所有线程是否执行完毕 必须放在shutdown后 while (!executor.isTerminated()) { Thread.sleep(1000); } long endTime = System.currentTimeMillis(); System.out.println("OVER!\t耗时: " + new DecimalFormat(".00").format((endTime - startTime) / 1000d) + " 秒"); } }
本文为原创文章,转载请注明出处!