使用Java从Google图像搜索下载图像

使用Java从Google图像搜索下载图像

本文介绍了使用Java从Google图像搜索下载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写Java代码,该代码应将Google图片搜索的结果提取给我.稍后,我还想在给定查询的情况下从Google图片搜索中下载所有图片.现在,我已经编写了这段代码,但是当我运行它时,它给出的错误是" JSONObject ["responseData"]不是JSONObject."

I am trying to write a Java code that should fetch me the results of the google image search. Later on, I also want to download all the images from Google Image search given a query. Right now, I have written this code but when I run it, it is giving the error that "JSONObject["responseData"] is not a JSONObject."

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import com.fasterxml.jackson.core.JsonParser;

import twitter4j.JSONObject;

public class TestImage {

public static void main(String[] args)
{
    try
    {
        URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
                "v=1.0&q=barack%20obama&userip=INSERT-USER-IP");
        URLConnection connection = url.openConnection();
        connection.addRequestProperty("Referer", "");

        String line;
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while((line = reader.readLine()) != null)
        {
            builder.append(line);
        }

        JSONObject json = new JSONObject(builder.toString());
        String imageUrl = json.getJSONObject("responseData").getJSONArray("results").getJSONObject(0).getString("url");
       // JsonParser jsonParser = new JsonParser();
        //((Object) jsonParser).parse(json).getAsJsonObject();
        BufferedImage image = ImageIO.read(new URL(imageUrl));
        JOptionPane.showMessageDialog(null, "", "", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(image));
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
    }
}

}`

由于我是Java的新手,请帮助我解决什么错误.

Please help me with what is the error as I am very new to Java.

推荐答案

这是因为不建议使用API​​,因此JSONObject ["resultData"]返回空值.

This is because the API is deprecated so JSONObject["resultData"] is returning a null value.

要确认这一点,只需输入搜索URL( https://ajax.googleapis .com/ajax/services/search/images ?" + "v = 1.0& q = barack%20obama& userip = INSERT-USER-IP)插入到Google的顶部栏中并对其进行谷歌搜索.您将看到Google不再提供图像数据.

To confirm this, simply enter your search URL (https://ajax.googleapis.com/ajax/services/search/images?" + "v=1.0&q=barack%20obama&userip=INSERT-USER-IP) into the top bar of your browser and google it. You will see that Google no longer provides the data for the images.

据我所知,虽然我听说过Google自定义搜索( https://developers.google.com/custom-search/json-api/v1/reference/cse/list )可以工作(尽管只有您每天获得100个免费查询,之后必须付费).

To my knowledge, there is not yet a very good replacement, although I've heard the Google Custom Search (https://developers.google.com/custom-search/json-api/v1/reference/cse/list) can work (although you only get 100 free queries per day and must pay after that).

这篇关于使用Java从Google图像搜索下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:43