首页 文章

如何使用JSoup来导航页面

提问于
浏览
0

我正在开发一个Android应用程序,它解析一个html页面并使用它的元素,然后转到下一页并提取其元素 . 我之前和Selenium一起做这个工作,但是当我在Android上使用它时,它使用了太多的内存,应用程序停止响应 . 现在我被困在登录页面,我必须输入用户名和密码 . 我设法做到了,但我无法进入下一页 . 它返回相同的登录页面而不是下一页 . 我真的需要用JSoup来做这件事,因为最终代码必须与android应用程序代码集成 . 请帮忙!

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();
    }
}

1 回答

  • 2
    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();
            }
        }
    }
    

相关问题