【问题标题】:Retrieving Data in Java在 Java 中检索数据
【发布时间】:2024-04-21 20:15:01
【问题描述】:

我是java新手。是否可以从网站获取数据,然后将其存储在某种数据结构中?例如,该程序在给定时间从 yahoo Finance 获取股票的价值并将其存储。就像我说的那样,我对 Java 不是很精通,我想知道这是否可以做到。如果可以,是不是很难做到?

【问题讨论】:

  • 是的,它可以做到,是的,这很困难(从你的感知)。您将需要了解诸如如何处理 URLConnection、基本 I/O 和 String 解析等内容。您甚至可能需要了解 JDBC。有了一些经验,不,这并不难。不过,我会从一些基础知识开始。尝试读取本地驱动器的 HTML 文件。一旦你了解了String解析的基础知识,请尝试下载它。
  • 您在谈论网络抓取。 Java中有一个很好的库可以帮助你做到这一点,叫做JSoup。
  • 很有可能,您尝试从中获取它的网站已经以编程方式更容易检索到 Web 服务,您无需进行繁重的 HTML 解析即可抓取这些服务

标签: java data-structures web-scraping data-extraction


【解决方案1】:
    public class GetYahooData
    {
        public ArrayList<JSONObject> getOutputFromUrl(String url) 
        {
            ArrayList<JSONObject> output = new ArrayList<JSONObject>();
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse response;
            StringBuilder builder= new StringBuilder();
            JSONObject myjson ;
            JSONArray the_json_array;
            try 
            {
                response = httpClient.execute(httpPost);
                BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                char[] buf = new char[8000];
                int l = 0;
                    while (l >= 0) 
                    {
                        builder.append(buf, 0, l);
                        l = in.read(buf);
                    }
                myjson = new JSONObject("{child:"+builder.toString()+"}");
                JSONObject mmm = new JSONObject(builder.toString());
                JSONArray mmmArr = mmm.getJSONArray("status");
                the_json_array = myjson.getJSONArray("child");
                for (int i = 0; i < the_json_array.length(); i++) 
                {
                    JSONObject another_json_object =  the_json_array.getJSONObject(i);//the_json_array.getJSONObject(i);
                    output.add(another_json_object);
                }
            } catch (ClientProtocolException e) {
                System.out.println("ClientProtocolException :"+e);
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("IOException :"+e);
                e.printStackTrace();
            } catch (JSONException e) {
                System.out.println("JSONException hussain :"+e);
                e.printStackTrace();
            }
            return output;
        }
    }

public class useYahoo
{
    public static void main(String args[])
    {
        String url = "the url you want the response from";
        getYahooData object = new GetYahooData();
        ArrayList<JSONObject> output = object.getOutputFromUrl(url);
    }
}

【讨论】:

    【解决方案2】:

    我已经广泛使用JSoup。如果您只需要自定义一个程序来从一个布局或结构不经常变化的网站中提取数据,JSoup 就足够了。

    假设您了解有关如何编程的基础知识(不一定熟悉 Java)并了解 Web 的组成部分(例如,htmldom 等是什么),我希望您选择快速了解如何使用JSoup 进行网页抓取。

    【讨论】:

      【解决方案3】:

      是的,您可以将任意网页下载到 Java 字符串中并解析内容,但是这样的解决方案并不可靠。如果作者更改网站的结构,您的代码将立即中断。

      进行此类集成的流行方式是RESTful web service。数据提供者将有一组 URL 和参数,您可以调用它们,并返回股票价格数据(以 xml 或 JSON 格式)

      【讨论】:

        【解决方案4】:

        是的,这可以在 web 服务的帮助下实现。

        1. Yahoo 或其他将公开 Web 服务。
        2. 您的程序将使用该 Web 服务并获取数据并在您结束时进行操作

        【讨论】: