当我在网站上抓取元素时,我的代码没有返回任何内容。我之前已经成功抓取过该网站上的表格。但是,我似乎无法从 Fantasy.premierleague.com 上的这些“动态表”中提取代码。
<强> http://i.imgur.com/cHkFwHE.png <我试图提取的内容的图片。
此外,我的登录详细信息在代码中合法显示,因此你们可以使用我的凭据登录并亲自查看发生了什么。 (显然这是一个备用的模拟帐户)。
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);
}
}
}
最佳答案
内容是动态创建的,您也可以使用selenium “get”来获取所请求的数据,然后您将能够解析它(使用 Jsoup 或其他东西)。
关于java - 发现很难从表中提取数据(JSoup),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31633195/