[英]Auto login on website, stay logged in, and parse with Jsoup (java)
for the last 3 weeks i was to trying to write a programm, which logs onto a website and loops through the pages to filter specific informations (specifis rows/columns of tables). 在过去的三个星期中,我试图编写一个程序,该程序登录到网站上并循环浏览页面以过滤特定信息(指定的行/表列)。 To be fair, this programm is the reason which i though myself coding (in java). 公平地说,这个程序是我自己(用Java编写)的原因。 I created some kind of an autofiller, which works, but is very slow, since it has to login for every page again. 我创建了一种自动填充程序,该方法可以运行,但是速度很慢,因为它必须再次为每个页面登录。 Therefore i was thinking, why my first (following) program isn't working. 因此,我在想,为什么我的第一个(以下)程序无法正常工作。 For some reason im able to log in, but as soon as i switch from the login page to the specific page (which is only accessable when logged in), i am being redirected to the login page. 由于某种原因我无法登录,但是一旦我从登录页面切换到特定页面(仅在登录时才可以访问),我就会被重定向到登录页面。 For the purpose of this question i created a fake account. 为了这个问题,我创建了一个假账户。 Maybe someone can help or tell where, where i can read further into this topic. 也许有人可以帮助或告诉我在哪里可以进一步阅读本主题。 I guess there is a problem with the cookies, though im not sure. 我猜饼干有问题,尽管我不确定。
try {
String url1 = "https://www.novaragnarok.com/";
String url2 = "https://www.novaragnarok.com/?module=vending&action=item&id=2499";
Connection.Response res = Jsoup
.connect(url1)
.followRedirects(true)
.data("username", "stackoverflowww", "password", "stackpw")
.method(Method.POST)
.execute();
Map<String, String> cookies = res.cookies();
Document doc = Jsoup.connect(url2)
.cookies(cookies)
.followRedirects(true)
.get();
System.out.println(cookies);
System.out.println(doc);
} catch (IOException e) {
e.printStackTrace();
}
Try this 尝试这个
String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)" +
" Chrome/56.0.2924.87 Safari/537.36";
public void parseWebsite(){
try{
Connection.Response homepage = Jsoup.connect("https://www.novaragnarok.com/").userAgent(USER_AGENT)
.method(Connection.Method.GET).timeout(6000).execute();
Connection.Response login = Jsoup.connect("https://www.novaragnarok.com//?module=account&action=login&return_url=")
.cookies(homepage.cookies()).data("txtbox", "stackoverflowww")
.data("password", "stackpw").userAgent(USER_AGENT).method(Connection.Method.POST)
.timeout(6000).execute();
Connection.Response url2 = Jsoup.connect("https://www.novaragnarok.com/?module=vending&action=item&id=2499")
.cookies(login.cookies()).userAgent(USER_AGENT).method(Connection.Method.GET).timeout(6000).execute();
//Your Code here
}catch (SocketException e){
e.printStackTrace();
}
catch (UncheckedIOException e){
e.printStackTrace();
}
catch(Exception e){
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.