简体   繁体   English

登录页面,然后使用JSOUP解析html

[英]Login to page then parse html using JSOUP

I currently have an app that takes a username & password from the user and then uses javascript commands on webview to log them in and present the page. 我目前有一个应用程序,该应用程序从用户那里获取用户名和密码,然后在webview上使用javascript命令将其登录并显示页面。 I now want to avoid using a webview, so Im planning to use jsoup to pull the necessary parts of the page and present that using something such as a textview instead. 我现在想避免使用Webview,因此Im打算使用jsoup提取页面的必要部分,并使用诸如textview之类的东西来呈现它。 So how can I use jsoup to execute javascript to log the user in, then scrape the html? 那么,如何使用jsoup执行javascript来登录用户,然后抓取html?

Jsoup can't execute javascript, since it doesn't include a javascript engine. Jsoup无法执行javascript,因为它不包含javascript引擎。 What you can use is the web driver api of selenium. 您可以使用Selenium的Web驱动程序api。 There is a new project now called android web driver that could be proved the right tool for you (never tried it myself). 现在有一个名为android web driver的新项目,可以证明它是适合您的工具(我自己从未尝试过)。

Links: 链接:

Android web driver Android网络驱动

Selenium web driver Selenium Web驱动程序

Jsoup does not execute JavaScript. Jsoup不执行JavaScript。 But I think you could still use jsoup in this case. 但是我认为您仍然可以在这种情况下使用jsoup。 It depends on how that JavaScript works. 这取决于JavaScript的工作方式。 In general it'll be like doing a page submit to some URL with credentials in POST data. 通常,这就像使用POST数据中的凭据将页面提交到某个URL一样。 So you need to identify the parameter name and value for both username and password. 因此,您需要标识用户名和密码的参数名称和值。 Then you could use Jsoup to post your data to that URL with appropriate credentials. 然后,您可以使用Jsoup使用适当的凭据将数据发布到该URL。

You could try something like 您可以尝试类似

Document doc = Jsoup.connect("http://www.yoururl.com/")
            .data("userName", "your username")
            .data("password", "your password")
            .post();

Then with the doc object you can parse and display your view. 然后,使用doc对象,您可以解析并显示您的视图。

You should first logon and then parse the html. 您应该先登录,然后解析html。 You can use cookies for logins and re-call the cookies in your further codes, like that. 您可以使用cookie进行登录,并使用您的其他代码重新调用cookie。

        Response res = Jsoup.connect("http://yourloginURL").data("username", "username", "password", "password").method(Method.POST).execute();
    Map<String, String> loginCookies = res.cookies();

    Document doc1 = Jsoup.connect("http://yourotherURL").cookies(loginCookies).get();

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

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