【问题标题】:How to collect (get and parse) the required information/data from a HTTP website?如何从 HTTP 网站收集(获取和解析)所需的信息/数据?
【发布时间】:2013-05-31 00:19:30
【问题描述】:

我有一个问题,自过去两周以来一直无法解决。我在这里需要一些帮助。我实际上想从 HTTP 网站获取和使用一些有用的数据。该网站实际上包含事故、事件和有关它们的所有信息。我想从网站上获取这些信息。我将在我的 Android 应用程序中使用它。我已经问过这个问题,但仍然无法解决。有人告诉我,你必须从 JSON 中获取这些数据。我以前没有这样做过。如果这是唯一的解决方案,那么我该怎么做。如果还有其他简单的方法,请给我。我实际上已经通过使用

获得了所有网站内容
private String DownloadText(String URL) {
    int BUFFER_SIZE = 2000;
    InputStream in = null;
    try {
        in = OpenHttpConnection(URL);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        return "exception in downloadText";
    }

    InputStreamReader isr = new InputStreamReader(in);
    int charRead;
    String str = "";
    char[] inputBuffer = new char[BUFFER_SIZE];          
    try {
        while ((charRead = isr.read(inputBuffer))>0)
        {                    
            //---convert the chars to a String---
            String readString = String.copyValueOf(inputBuffer, 0, charRead);
            str += readString;
            inputBuffer = new char[BUFFER_SIZE];
        }
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    }    
    return str;        
}

private InputStream OpenHttpConnection(String urlString) throws IOException {

    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))                     
        throw new IOException("Not an HTTP connection");

    try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect(); 

        response = httpConn.getResponseCode();                 
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();                                 
        }                     
    }
    catch (Exception ex) {
        throw new IOException("Error connecting");            
    }
    return in;     
}

但它提供了所有内容,即所有 info+html+xml+++。但我只想要必需的信息。

另一件事是,在获取该数据之前是否必须获得网站管理员权限?

【问题讨论】:

    标签: android xml-parsing html-parsing android-parser


    【解决方案1】:

    您正在寻找的是一种称为 web 抓取或 html 抓取的东西。 看看这个 SO question 让你开始: Options for HTML scraping?

    【讨论】:

      猜你喜欢
      • 2012-02-21
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 2012-01-22
      相关资源
      最近更新 更多