Jsoup直接链接解析不起作用 [英] Jsoup parsing from direct link doesn't work

查看:222
本文介绍了Jsoup直接链接解析不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要接收来自网站的内容。一般来说它的作品不错,但我有另外一个问题。网页主要有网页,其中一个画面是在中间,点击后,使我们到另一页。我试图表明,从直接链接的内容,但我总是收到来自这个主页的内容不是从我想要的页面。我用jsoup库。是否有可能解决此问题?我的code:

 私有类解析器扩展的AsyncTask<太虚,太虚,太虚> {
    串H;
    字符串URL =htt​​p://www.klt.net.pl/index.php?a=ostatnie_kolejki;    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        PD =新ProgressDialog(MainActivity.this);
        pd.setTitle(分析器);
        pd.setMessage(正在加载...);
        pd.setIndeterminate(假);
        pd.show();
    }    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
        尝试{
            文献文件= Jsoup.connect(URL)获得();
            H = document.html();
        }赶上(IOException异常五){
            e.printStackTrace();
        }
        返回null;
    }    @覆盖
    保护无效onPostExecute(虚空结果){
        r.setText(H);
        pd.dismiss();
    }
}


解决方案

我曾尝试访问您的网址,并通过检查网络选项卡中,我发现,您要访问的页面需要PHPSESSIONID,这是从响应的cookie与图像的页面。所以,请尝试以下code,它应该工作:)

  @覆盖
保护无效doInBackground(虚空...... PARAMS){
    尝试{
        Connection.Response响应= Jsoup.connect(URL)
            。方法(Connection.Method.GET)
            .timeout(50000)
            .followRedirects(真)
            。执行();
        文献文件= Jsoup.connect(http://www.klt.net.pl/index.php)
            .cookies(response.cookies())
            。得到();
        H = document.html();
    }赶上(IOException异常五){
        e.printStackTrace();
    }
    返回null;
}

I need to receive content from website. Generally it works good but I have problem with other one. Webpage has main page where one picture is in the middle and after clicking it, moves us to the another page. I try to show content from direct link but always I receive content from this main page not from page which I want. I use jsoup library. Is there any possibility to solve this? My code:

private class Parser extends AsyncTask<Void, Void, Void> {
    String h;
    String url = "http://www.klt.net.pl/index.php?a=ostatnie_kolejki";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(MainActivity.this);
        pd.setTitle("Parser");
        pd.setMessage("Loading...");
        pd.setIndeterminate(false);
        pd.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Document document = Jsoup.connect(url).get();
            h = document.html();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        r.setText(h);
        pd.dismiss();
    }
}

解决方案

I have tried accessing your URL, and by inspecting the network tab I found out that the page you want to access requires a PHPSESSIONID, which is a response cookie from the page with the image. So please try the code below, it should work :)

@Override
protected Void doInBackground(Void... params) {
    try {
        Connection.Response response = Jsoup.connect(url)
            .method(Connection.Method.GET)
            .timeout(50000)
            .followRedirects(true)
            .execute();
        Document document = Jsoup.connect("http://www.klt.net.pl/index.php")
            .cookies(response.cookies())
            .get();
        h = document.html();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

这篇关于Jsoup直接链接解析不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆