简体   繁体   English

如何使用JSoup导航页面

[英]How to use JSoup to navigate pages

I'm working on an android app which parses an html page and uses its elements and then goes to the next page and extracts its elements. 我正在研究一个解析html页面并使用其元素的android应用,然后转到下一页并提取其元素。 I was previously doing this work with Selenium but when I use it with Android it uses way too much memory and the app stops responding. 我以前是用Selenium来做这项工作的,但是当我在Android上使用它时,它会占用过多的内存,并且应用程序停止响应。 Now I'm stuck at the login page where I have to enter the username and password. 现在,我被困在登录页面中,必须输入用户名和密码。 I manage to do that but I'm not able to go to the next page. 我设法做到了,但无法转到下一页。 It returns me the same login page and not the next one. 它会返回相同的登录页面,而不是下一个。 I really need to do this with JSoup because eventually the code has to be integrated with the android app code. 我真的需要用JSoup做到这一点,因为最终代码必须与android应用程序代码集成在一起。 Help please! 请帮助!

 try {


        String url = "http://slateisb.nu.edu.pk/portal";
        Document doc = Jsoup.connect(url).
                followRedirects(true).
                data("eid", "i110013").
                data("pw", "001").
                method(Method.POST).get();
        String title = doc.title();
        print("Title : %s" , title);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Main {

    public static void main(String[] args) {
        try {
            //In this url you must login
            String loginUrl = "http://slateisb.nu.edu.pk/portal/xlogin";

            //This is an example, it can be anything else
            String url = "http://slateisb.nu.edu.pk/portal";

            //First login. Take the cookies
            Connection.Response res = Jsoup
                    .connect(loginUrl)
                    .data("eid", "i110013")
                    .data("pw", "001")
                    .referrer("http://www.google.com")
                    .userAgent(
                            "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .method(Method.POST).timeout(0).execute();

            Map<String, String> loginCookies = res.cookies();

            //Now you can parse any page you want, as long as you pass the cookies
            Document doc = Jsoup
                    .connect(url)
                    .timeout(0)
                    .cookies(loginCookies)
                    .referrer("http://www.google.com")
                    .userAgent(
                            "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .get();

            System.out.println("Title : " + doc.title());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM