JSoup 如何解析 table 3 行
JSoup how to parse table 3 rows
我有一个像这样的 table,我想对其进行解析以获取 row.id
的数据代码值以及 table.[=17 的第二列和第三列=]
<table>
<tr class="id" data-code="100">
<td></td>
<td>18</td>
<td class="name">John</td>
<tr/>
<tr class="id" data-code="200">
<td></td>
<td>21</td>
<td class="name">Mark</td>
<tr/>
</table>
我要打印出来。
100, 18, John
200, 21, Mark
我尝试了此线程中的以下建议,但没有选择任何内容 how to parse a table from HTML using jsoup
URL url = new URL("http://www.myurl.com");
Document doc = Jsoup.parse(url, 3000);
Element tables = doc.select("table[class=id]");
for(Element table : tables)
{
System.out.println(table.toString());
}
编辑:也尝试使用 Jsoup.connect() 而不是 parse()
Document doc = null;
try
{
doc = Jsoup.connect("http://www.myurl.com").get();
}
catch (IOException e)
{
e.printStackTrace();
}
这样试试:
URL url = new URL("http://www.myurl.com");
Document doc = Jsoup.parse(url, 3000);
// This should work now
Element tables = doc.select("table tr .id");
// This propably should work too
Element tables2 = doc.select("table tr[class*=id]");
for(Element table : tables)
{
System.out.println(table.toString());
}
来自文档:
public Elements select(String cssQuery) Find elements that match the
Selector CSS query, with this element as the starting context. Matched
elements may include this element, or any of its children. This
method is generally more powerful to use than the DOM-type
getElementBy* methods, because multiple filters can be combined, e.g.:
•el.select("a[href]") - finds links (a tags with href attributes)
•el.select("a[href*=example.com]") - finds links pointing to
example.com (loosely)
See the query syntax documentation in Selector.
Parameters: cssQuery - a Selector CSS-like query Returns: elements
that match the query (empty if none match)
我有一个像这样的 table,我想对其进行解析以获取 row.id
的数据代码值以及 table.[=17 的第二列和第三列=]
<table>
<tr class="id" data-code="100">
<td></td>
<td>18</td>
<td class="name">John</td>
<tr/>
<tr class="id" data-code="200">
<td></td>
<td>21</td>
<td class="name">Mark</td>
<tr/>
</table>
我要打印出来。
100, 18, John
200, 21, Mark
我尝试了此线程中的以下建议,但没有选择任何内容 how to parse a table from HTML using jsoup
URL url = new URL("http://www.myurl.com");
Document doc = Jsoup.parse(url, 3000);
Element tables = doc.select("table[class=id]");
for(Element table : tables)
{
System.out.println(table.toString());
}
编辑:也尝试使用 Jsoup.connect() 而不是 parse()
Document doc = null;
try
{
doc = Jsoup.connect("http://www.myurl.com").get();
}
catch (IOException e)
{
e.printStackTrace();
}
这样试试:
URL url = new URL("http://www.myurl.com");
Document doc = Jsoup.parse(url, 3000);
// This should work now
Element tables = doc.select("table tr .id");
// This propably should work too
Element tables2 = doc.select("table tr[class*=id]");
for(Element table : tables)
{
System.out.println(table.toString());
}
来自文档:
public Elements select(String cssQuery) Find elements that match the Selector CSS query, with this element as the starting context. Matched elements may include this element, or any of its children. This method is generally more powerful to use than the DOM-type getElementBy* methods, because multiple filters can be combined, e.g.: •el.select("a[href]") - finds links (a tags with href attributes) •el.select("a[href*=example.com]") - finds links pointing to example.com (loosely)
See the query syntax documentation in Selector.
Parameters: cssQuery - a Selector CSS-like query Returns: elements that match the query (empty if none match)