Загрузите URL в java, "прокручиваемом" до заданного места - PullRequest
0 голосов
/ 10 сентября 2018

Я создал программу, которая загружает все картинки на изображениях Google по ключевому слову в java (для обучения нейронной сети) и загружает первые 30 или 50 картинок, потому что это картинки, загружаемые по URL.Я не знаю, как сделать так, чтобы «прокрутка» пошла вниз, чтобы загрузить больше фотографий.

здесь, если я как загрузить свой URL:

URL urlObject = new URL(url);
URLConnection urlConnection = urlObject.openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

Спасибо за помощь.

PS и это все моя программа, если это может помочь:

public class TrainingDataSetDownload {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Image image = null;
        String urlSource;
        Scanner scan = new Scanner(System.in);
        String toSearch;
        int index = -4;
        char tempChar;
        int tempIndex;
        String toDownload;
        int iImage = 0;
        File destinationFile;
        try {

            System.out.print("search data: ");
            toSearch = scan.nextLine();
            urlSource = getURLSource("https://www.google.com/search?hl=fr&tbm=isch&source=hp&biw=1102&bih=498&ei=FlWWW63JJsW7sQGFxy0&q=" + toSearch);
            //System.out.println(urlSource);
            String dirName = "C:\\Users\\ab\\Desktop\\programation\\" + toSearch + "\\";
            File dir = new File(dirName);
            boolean isCreated = dir.mkdirs();
            destinationFile=new File(dirName);
            destinationFile.createNewFile();

            while(index != -1){
                toDownload = "";
                tempChar = " ".charAt(0);
                tempIndex = 0;
                index = urlSource.indexOf(".jpg", index + 4);
                tempIndex = index;
                iImage += 1;
                while(tempChar != "\"".charAt(0)){
                    tempIndex--;
                    tempChar = urlSource.charAt(tempIndex);
                }
                for(int i = tempIndex + 1; i < index + 4; i++){
                    toDownload = toDownload.concat(Character.toString(urlSource.charAt(i)));
                }
                try{
                URL url = new URL(toDownload);
                image = ImageIO.read(url);
                File nomfichier = new File(dirName + iImage + ".jpg");
                BufferedImage bi = imageToBufferedImage(image);
                ImageIO.write(bi, "png", nomfichier);
                System.out.println("-----------------------------------------");
                System.out.println("image download: " + toDownload);
                System.out.println("at index : " + tempIndex);
                System.out.println("-----------------------------------------");
                }
                catch(Exception e){
                    System.out.println(e);
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static BufferedImage imageToBufferedImage(Image im) {
     BufferedImage bi = new BufferedImage
        (im.getWidth(null),im.getHeight(null),BufferedImage.TYPE_INT_RGB);
     Graphics bg = bi.getGraphics();
     bg.drawImage(im, 0, 0, null);
     bg.dispose();
     return bi;
  }

  public static String getURLSource(String url) throws IOException
    {
        URL urlObject = new URL(url);
        URLConnection urlConnection = urlObject.openConnection();
        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

        return toString(urlConnection.getInputStream());
    }

    public static String toString(InputStream inputStream) throws IOException
    {
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")))
        {
            String inputLine;
            StringBuilder stringBuilder = new StringBuilder();
            while ((inputLine = bufferedReader.readLine()) != null)
            {
                stringBuilder.append(inputLine);
            }

            return stringBuilder.toString();
        }
    }  

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...