如何使用Java jsoup连接到站点并检索数据? [英] How to connect to the site and retrieve data using java jsoup?
问题描述
我知道那里有很多信息,但是找不到适合我问题的信息.我想从需要登录的页面收集数据.这是我正在尝试做的事情:
I know there's a lot of info out there but I couldn't find anything fitting to my problem. I want to gather data from a page I need to be logged in. Here is what I'm trying to do:
我收集Cookies来做
I gather Cookies doing:
Connection.Response res = Jsoup
.connect("http://website.com/login?event=doLogin")
.execute();
Map <String,String> cookies = res.cookies();
然后读取html以获取隐藏值:
And then read the html for the hidden values:
Document doc = Jsoup
.connect("http://website.com/login?event=doLogin")
.cookies(cookies)
.get();
html = doc.toString();
length = html.length();
counter = 0;
for (int i = 0; i < length; i++) {
if (html.startsWith("document.write", i)){
name[counter] = html.substring(i + 41, i + 144);
value[counter] = "Login";
counter++;
}
if (html.startsWith("hidden", i)) {
name[counter] = html.substring(i + 13, i + 81);
value[counter] = html.substring(i + 90, i + 123);
counter++;
}
}
最后,我想使用此信息通过Cookie和隐藏值登录:
Finally I want to use this information to login using Cookies and hidden values:
Document doc2 = Jsoup
.connect("http://website.com/login?event=doLogin")
.cookies(cookies)
.data("email", "my@email")
.data("pass", "mypass")
.data(name[0], value[0])
.data(name[1], value[1])
.data(name[2], value[2])
.method(Connection.Method.POST)
.get();
System.out.println(doc2);
但是我所得到的只是登录页面. 恐怕尝试时可以更改那些隐藏的值:
But all I got is the login page. I'm afraid those hidden values can be changed when I try:
Document doc2 = Jsoup.connect
我做对了吗?
推荐答案
将方法设置为POST然后调用GET请求时,这是一种混合上下文. 试试这个:
It's kind of mixed context when you set method to POST and then call GET request. Try this:
Connection.Response res = Jsoup.connect("http://website.com/login?event=doLogin")
.execute();
...
Document doc = Jsoup.connect("http://website.com/login?event=doLogin")
.cookies(res.cookies())
.data("email", "my@email")
.data("pass", "mypass")
.data(name[0], value[0])
.data(name[1], value[1])
.data(name[2], value[2])
.post();
这篇关于如何使用Java jsoup连接到站点并检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!