【问题标题】:JSoup selector not returning resultJSoup 选择器不返回结果
【发布时间】:2016-05-03 18:36:58
【问题描述】:

我过去曾使用过 JSoup,并且能够将 XPath 转换为选择器,但我无法在已更改的站点上获得此表的结果。

我正在尝试从this Georgia soccer site 检索排名表。

在 Chrome 中,我将表格 tbody 的选择器设为 #tabs-1 > div > table:nth-child(12) > tbody > tr > td > table:nth-child(1) > tbody > tr:nth-child(1) > td > table > tbody

Firefox 为表 .placeholder > table:nth-child(12) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > table:nth-child(1) 提供了类似的选择器

两者都不起作用。即使在Try JSoup link

第一个第 n 个子表本身 #tabs-1 > div > table:nth-child(12) 返回 null。

知道这个页面有什么特别之处吗?

我将用户代理字符串设置为Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36

【问题讨论】:

  • 似乎尝试 jsoup 解析 HTML 的方式与浏览器不同。根据在两个浏览器中看到的 DOM,很明显有一个#tabs-1 > div > table:nth-child(12)。该页面充满了错误,所以我不会感到惊讶。
  • 我在 github 上向 Jsoup 团队报告了一个问题 (github.com/jhy/jsoup/issues/705)。

标签: css-selectors jsoup


【解决方案1】:

我认为您在 Jsoup 中发现了一个错误。但是,这里有一个提取表的解决方法:

table#Table6 + table > tbody:has(tr.theadb)

DEMO

【讨论】:

    【解决方案2】:

    我认为您需要的信息在类名为 tbody0 和 tbody1 的 tr 标记中。你可以试试这个。

    public static void main (String []args) throws IOException {
     Document doc = Jsoup.connect("http://gs-fall15athenaandclassic.sportsaffinity.com/tour/public/info/schedule_results2.asp?sessionguid=&flightguid=CB37D3C7-5EF7-4CCE-8918-3AC3083495A3&tournamentguid=173EB212-BADB-425F-BE03-D05CA448AD8D").get();
     Elements ele = doc.select("tr.tbody0,tr.tbody1");
    
     for(Element e : ele){
         System.out.println(e.text()+",");        
     }
     }
    

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 2018-08-11
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      相关资源
      最近更新 更多