Java 使用JSoup获取错误。为什么?
我正在尝试从fantasyfootball网站登录并提取数据 我得到以下错误 2015年7月24日晚上8:01:12 StatCollector干管 严重:空 org.jsoup.HttpStatusException:获取URL时出现HTTP错误。状态=403,URL= 位于org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:537) 位于org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:493) 位于org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205) 位于statcollector.main(statcollector.java:26) 每当我尝试这个代码时。我哪里做错了Java 使用JSoup获取错误。为什么?,java,jsoup,Java,Jsoup,我正在尝试从fantasyfootball网站登录并提取数据 我得到以下错误 2015年7月24日晚上8:01:12 StatCollector干管 严重:空 org.jsoup.HttpStatusException:获取URL时出现HTTP错误。状态=403,URL= 位于org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:537) 位于org.jsoup.helper.HttpConnection$Res
public class StatsCollector {
public static void main (String [] args){
try {
String url = "http://fantasy.premierleague.com/";
Connection.Response response = Jsoup.connect(url).method(Connection.Method.GET).execute();
Response res= Jsoup
.connect(url)
.data("ismEmail", "example@googlemail.com", "id_password", "examplepassword")
.method(Method.POST)
.execute();
Map<String, String> loginCookies = res.cookies();
Document doc = Jsoup.connect("http://fantasy.premierleague.com/transfers")
.cookies(loginCookies)
.get();
String title = doc.title();
System.out.println(title);
}
catch (IOException ex) {
Logger.getLogger(StatsCollector.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
公共类StatCollector{
公共静态void main(字符串[]args){
试一试{
字符串url=”http://fantasy.premierleague.com/";
Connection.Response-Response=Jsoup.connect(url).method(Connection.method.GET).execute();
响应res=Jsoup
.connect(url)
.数据(“ISMAIL”example@googlemail.com“,“id\U密码”,“示例密码”)
.method(method.POST)
.execute();
Map loginCookies=res.cookies();
Document doc=Jsoup.connect(“http://fantasy.premierleague.com/transfers")
.cookies(loginCookies)
.get();
字符串title=doc.title();
系统输出打印项次(标题);
}
捕获(IOEX异常){
Logger.getLogger(statCollector.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
您是否正在尝试执行此实际代码?这似乎是一个带有占位符而不是登录凭据的示例代码。这将解释您收到的错误
编辑1
我的错。我查看了该网站上的登录表单,在我看来,您将输入元素的id
(“ISMAIL”和“id\u密码”)与表单中发送的名称(“email”,“password”)混淆了。这对您有用吗
Response res= Jsoup
.connect(url)
.data("email", "example@googlemail.com", "password", "examplepassword")
.method(Method.POST)
.execute();
编辑2
好吧,这在我脑子里一直想不起来,因为用JSoup登录网站应该没那么难。我在那里创建了一个帐户,并为自己做了尝试。代码优先:
String url = "https://users.premierleague.com/PremierUser/j_spring_security_check";
Response res = Jsoup
.connect(url)
.followRedirects(false)
.timeout(2_000)
.data("j_username", "<USER>")
.data("j_password", "<PASSWORD>")
.method(Method.POST)
.execute();
Map<String, String> loginCookies = res.cookies();
Document doc = Jsoup.connect("http://fantasy.premierleague.com/squad-selection/")
.cookies(loginCookies)
.get();
stringurl=”https://users.premierleague.com/PremierUser/j_spring_security_check";
响应res=Jsoup
.connect(url)
.followRedirects(false)
.超时(2_000)
.data(“j_用户名”,即“”)
.data(“j_密码”和“”)
.method(method.POST)
.execute();
Map loginCookies=res.cookies();
Document doc=Jsoup.connect(“http://fantasy.premierleague.com/squad-selection/")
.cookies(loginCookies)
.get();
那么这里发生了什么呢?首先,我意识到登录表单的目标是错误的。页面似乎是建立在spring上的,因此表单属性和目标使用spring默认值j_-spring\u-security\u-check
,j_-username
和j_-password
。然后,我出现了一个读取超时,直到我设置了标志followRedirects(false)
。我只能猜测这有什么帮助,但也许这是对爬虫的保护
最后,我尝试连接到“阵容选择”页面,解析后的响应包含我的个人视图和数据。这段代码似乎对我有效,您可以尝试一下吗?您是否尝试了解http status 403的含义?您应该附加完整的异常,而不仅仅是一条断章取义的错误消息。我想我已经做了什么你的意思是@javahippieit是一个受限资源。哈哈,不。我正在使用我的实际凭据。尽管如此,Web服务器正在>>告诉我我明白了。我将代码更改为你所说的,它仍然给我一个403错误。也许它告诉我不允许我提出那个请求(就像你说的那样).有什么解决办法吗?只是猜测一下:您是否有任何客户id或wile注册的令牌?如果有,请尝试在url被拆分后追加?@HishamMohammedRight现在您正在将POST请求发送到普通http://fantasy.premierleague.com
page,表单的目标是https://users.premierleague.com/Premier用户/重定向登录
。这会改变什么吗?
String url = "https://users.premierleague.com/PremierUser/j_spring_security_check";
Response res = Jsoup
.connect(url)
.followRedirects(false)
.timeout(2_000)
.data("j_username", "<USER>")
.data("j_password", "<PASSWORD>")
.method(Method.POST)
.execute();
Map<String, String> loginCookies = res.cookies();
Document doc = Jsoup.connect("http://fantasy.premierleague.com/squad-selection/")
.cookies(loginCookies)
.get();