且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Jsoup要求登录的Java抓取网站

更新时间:2023-12-03 21:27:10

您的代码无法将您登录到网站....请尝试以下代码登录该网站.

Your code doesn't log you in to the website....Try the below code to login to the website.

要登录网站:

Connection.Response res = Jsoup.connect(SPLIT_LOGIN)
            .data("action", "account", 
                "redirect", "account_home.php?",
                "radiobutton", "old", 
                "loginemail", "XXXXX",
                "password", "XXXXX", 
                "LoginChoice", "Sign In to Secure Area")
            .method(Connection.Method.POST)
            .followRedirects(true)
            .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36")
            .execute();

因此,您现在已经登录,但是该网站似乎检测到您是在其他浏览器还是在连接中登录,因此要求您先终止该连接.因此,下面是终止连接的代码:

So you are now logged in, however the website seems to detect whether you are logged in in other browser or connection, requests that you terminate that connection first. So below is the code for terminating the connection:

Connection.Response res2 = Jsoup.connect("http://www.streetinsider.com/login_duplicate.php")
            .data("ok", "End Prior Session")
            .method(Connection.Method.POST)
            .cookies(res.cookies())
            .followRedirects(true)
            .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36")
            .execute();

一切顺利,现在 res2 将包含您帐户的主页,然后您可以继续转到所需的任何页面.有关如何使用 Jsoup 登录网站的更多信息,请查看以下教程:

All good, now res2 will contains the home page of your account, you can then proceed to go to whatever page you want. For more information on how to login to a website with Jsoup, take a look at the following tutorial:

如何使用Jsoup登录网站