【问题标题】:Getting data from a website, without API? [closed]从网站获取数据,无需 API? [关闭]
【发布时间】:2013-03-02 10:59:12
【问题描述】:

我想从此网站自动获取有关房地产的数据:

LINK

但是,他们没有 api。你一般会怎么做?感谢您的每一次回复!

【问题讨论】:

标签: java api rest web


【解决方案1】:

您将不得不自己下载页面,并自己解析所有信息。

您可能想查看Pattern 类,查看一些regexURLString 类将非常有用。

您总是可以下载一个 html 库以使其更容易。可能类似于http://htmlparser.sourceforge.net/

非常笼统的问题,很明显我无法提供相关代码,但这被称为抓取。

【讨论】:

  • 我必须下载它还是有什么方法可以发送http请求?
  • @user2051347 您可以请求任何您想要的信息,但它不仅会神奇地出现在您的数据中。我不确定你在问什么。
  • 我的意思是,我只是发送和 http 请求,然后返回 html 页面,然后在代码中搜索关键字,而没有真正下载页面。
  • @user2051347 如果您不先将页面下载到程序中的某种类型的数据,您将搜索什么?您无法找到您的程序一开始就无法使用的关键字。
  • @user2051347 您确实需要了解 HTTP、HTML 和 WWW 的工作原理。
【解决方案2】:

这就是您从页面获取所有内容的方式

那么你就可以根据需要解析页面数据了

package farzi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;

import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

public class GetXMLTask
{
    public static void main(String args[]) 
    {
        try 
        {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://derstandard.at/anzeiger/immoweb/Suchergebnis.aspx?Regionen=9&Bezirke=&Arten=&AngebotTyp=&timestamp=1363245585829");
            HttpResponse response;
            StringBuilder builder= new StringBuilder();
            response = httpClient.execute(httpPost);
            System.out.println(response.toString());
            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            char[] buf = new char[1000];
            int l = 0;
                while (l >= 0) 
                {
                    builder.append(buf, 0, l);
                    l = in.read(buf);
                }
                System.out.println(builder.toString());
        } 
        catch (URISyntaxException e) {
            System.out.println("URISyntaxException :"+e);
            e.printStackTrace();
        } 
        catch (HttpException e) {
            System.out.println("HttpException :"+e);
            e.printStackTrace();
        } 
        catch (InterruptedException e) {
            System.out.println("InterruptedException :"+e);
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IOException :"+e);
            e.printStackTrace();
        } 
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-14
    • 2013-03-10
    • 2016-05-03
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    相关资源
    最近更新 更多