java - 使用带有动态 ID 的 jsoup

标签 java css-selectors html-parsing jsoup

我正在尝试使用 jsoup 来解析 html 文件。它是使用表格来展示产品。每个产品都在一个表中,其 id 范围从 1 到“n”。就像下面的例子:

<table align="center" width="98%" id="A + 1">
   <tbody>
      <tr>
         <td valign="top" style="width: 03%;">
            <span class="line">1</span>
         </td>
         <td valign="top" style="width: 56%;">
            <span class="line">PRODNAME</span>
         </td>
         <td valign="top" style="width: 10%;">
            <span class="line">850.000</span>
         </td>
      </tr>
   </tbody>
</table>

因此,第一个产品将在表中使用 ID“A + 1”,第二个产品将在“A + 2”中,依此类推。

我无法使用选择器遍历这些表。我在做:

Document doc = Jsoup.parse(html);
Elements products = doc.select("table[idˆ=A]");
for (Element product : products) {
   // do something
}

如果我没看错(http://jsoup.org/apidocs/org/jsoup/select/Selector.html),doc.select("table[id^=A]") 应该检索所有 id 属性以“A”开头的表...

但是我的 Elements 对象(产品)是空的...我做错了什么?

我在装有 Netbeans 7.1.2 的 Mac OS X (10.7.4) 上使用 jsoup 1.6.3、java 1.6.0_31。

感谢任何帮助。

最佳答案

您似乎在选择器中使用了错误的抑扬符,尽管我不确定 jsoup 是否应该返回空结果集或在无效选择器上抛出异常。

无论如何,试试这个:

Document doc = Jsoup.parse(html);
Elements products = doc.select("table[id^=A]");
for (Element product : products) {
   // do something
}

关于java - 使用带有动态 ID 的 jsoup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10861066/

相关文章:

python - 提取 <div> 标签之外的文本 BeautifulSoup

python - 使用正则表达式解析字符串python3

java - 从挂起/hibernate 状态恢复计算机后如何告诉 ScheduledExecutorService 忽略重叠执行

java - 运行时处理jar文件

java - 从堆转储文件中识别堆内存的大小?

javascript - 使用 querySelectorAll 检索直接子项

html - 使用 CSS 伪类 :first-child

jquery - 使 css nth-child() 只影响可见

python - 使用 beautiful soup 4 抓取 <p class ="postbody"> 标签内的 URL 并将其保存到文本文件中

java - 如何在 Java 中删除属性及其值?