极简壁纸Java爬虫(伪
通过Robot类爬取极简壁纸
直接上代码,关于正经的爬虫,极简壁纸f12有限制,又是动态的,就不想慢慢分析,就用robot下载
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
public class main {
//进入
public static void main(String[] args) throws AWTException {
System.out.println("成功进入");
Robot robot = new Robot();
RobotUtil ru = new RobotUtil(new Robot());
new MyFrame();//增加按键监听来停止爬虫
//多少页
for(int a=0;a<20;a++) {
ru.clickMouse(550, 400, KeyEvent.BUTTON1_DOWN_MASK, 1, 500);
for (int i = 0; i < 24; i++) {
ru.clickMouse(750, 790, KeyEvent.BUTTON1_DOWN_MASK, 1, 500);
ru.clickMouse(1000, 790, KeyEvent.BUTTON1_DOWN_MASK, 1, 500);
}
ru.clickMouse(1500, 100, KeyEvent.BUTTON1_DOWN_MASK, 1, 500);
robot.mouseMove(1600, 100);
robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK);
robot.mouseMove(1600, 900);
robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);
ru.clickMouse(850, 720, KeyEvent.BUTTON1_DOWN_MASK, 1, 1000);
System.out.println(a);
}
}
}
class MyFrame extends JFrame {
/**
* @param args
*/
char charA;
public MyFrame(){
this.setSize(500,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("my jframe");
this.setVisible(false);
this.addKeyListener(new MyKeyListener());
}
class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_C){
System.exit(0);
}
}
}
}
封装的robot类
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class RobotUtil {
Robot robot;
public RobotUtil(Robot robot){
this.robot = robot;
}
public void clickKey(int Key,int sleep) throws InterruptedException {
robot.keyPress(Key);
Thread.sleep(sleep);
robot.keyRelease(Key);
}
public void clickMore(int[] keys,int sleep) throws InterruptedException {
for (int i : keys) {
robot.keyPress(i);
}
Thread.sleep(sleep);
for (int i : keys) {
robot.keyRelease(i);
}
}
public void clickMouse(int x,int y,int Key,int count,int sleep){
robot.mouseMove(x,y);
for(int i = 0;i<count;i++){
robot.mousePress(Key);
robot.mouseRelease(Key);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public boolean screenshot(int x,int y,int _X,int _Y,String file) throws FileNotFoundException {
BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(x, y, _X, _Y));
File file1 = new File(file);
OutputStream os = new FileOutputStream(file1);
try {
ImageIO.write(screenCapture,"png",os);
} catch (IOException e) {
return false;
}
return true;
}
}

浙公网安备 33010602011771号