【发布时间】:2015-07-26 02:56:08
【问题描述】:
当我在网站上抓取元素时,我的代码没有返回任何内容。我之前已经成功爬过这个网站上的表格。但是,我似乎无法从 Fantasy.premierleague.com 上的这些“动态表”中提取代码。
http://i.imgur.com/cHkFwHE.png
此外,我的登录详细信息合法地显示在代码中,因此你们可以使用我的凭据登录并亲自查看发生了什么。 (obv 这是一个备用的模拟账户)。
public class StatsCollector {
public static void main (String [] args){
try {
String url = "https://users.premierleague.com/PremierUser/j_spring_security_check";
Response res = Jsoup
.connect(url)
.followRedirects(false)
.timeout(2_000)
.data("j_username", "<fantasyfootball123@guerrillamail.com>")
.data("j_password", "<fantasy123>")
.method(Method.POST)
.execute();
Map<String, String> loginCookies = res.cookies();
String url1 = "http://fantasy.premierleague.com/stats/elements/?page=1" ;
Document doc = Jsoup.connect
(url1)
.cookies(loginCookies)
.get();
for (Element table: doc.select("table.ismEiwMatchesPast")) {
for (Element tbody: table.select("tbody.ismHistoryPastSeasons")) {
for (Element row: table.select("tr")){
Elements tds = row.select("td");
if (tds.size()>2){
System.out.println(tds.get(0).text() + " : " + tds.get(1).text() + " : " + tds.get(2).text());
}
}
}
}
}
catch (IOException ex) {
Logger.getLogger(StatsCollector.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
【问题讨论】:
-
我没有以你的身份登录,但在我看来,这些表可能是通过 AJAX 动态生成的。您可能应该尝试提取该 AJAX 调用的数据(通常是 JSON 或 XML 数据),而不是由 javascript 生成的 HTML。
-
@hisham mohammed 在你尝试获取 jtable 之前先尝试连接到 page。尝试到
res看看,有什么反应。