<table class="sparql" border="1">
<tbody><tr>
<th>simpleProperty</th>
</tr>
<tr>
<td><a href="http://www.wikidata.org/entity/P115c">http://www.wikidata.org/entity/P115c</a></td>
</tr>
</tbody></table>
使用Jsoup,我正在尝试收集来自this页面的所有链接。
我一直在尝试许多不同的方法,但我似乎无法把它固定下来。最近我尝试过这样:
// parse the input stream using Jsoup
docx = Jsoup.parse(wiki_relation_InputStream, null, wikidata_relation_page.getProtocol()+"://"+wikidata_relation_page.getHost()+"/");
Element table = doc.select("table").first(); //gets a table with the class "first class"
Elements links = table.select("a[href]");
看起来它应该很容易,因为结构是如此微小,但唉,但它给我带来了一些麻烦。
如果有多个我想收集它们的情况。在没有零的情况下,如果程序没有在死亡和破坏的火球中崩溃,我更愿意。
如何获取难以捉摸的链接文字? (例如在http://www.wikidata.org/entity/P115c
中)
更新
关于熊猫的建议//get it's normal wiki disambig page
String URL_czech = "http://milenio.dcc.uchile.cl/sparql?default-graph-uri=&query=PREFIX+%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%0D%0ASELECT+*+WHERE+%7B%0D%0A+++%3A"
+ home + "+%3FsimpleProperty+%3A"
+ away + "%0D%0A%7D%0D%0A&format=text%2Fhtml&timeout=0&debug=on";
URL wikidata_page = new URL(URL_czech);
HttpURLConnection wiki_connection = (HttpURLConnection)wikidata_page.openConnection();
InputStream wikiInputStream = null;
try
{
// try to connect and use the input stream
wiki_connection.connect();
wikiInputStream = wiki_connection.getInputStream();
}
catch(IOException error)
{
// failed, try using the error stream
wikiInputStream = wiki_connection.getErrorStream();
}
// parse the input stream using Jsoup
Document docx = Jsoup.parse(wikiInputStream, null, wikidata_page.getProtocol()+"://"+wikidata_page.getHost()+"/");
Elements link_text = docx.select("table.sparql > tbody > tr:nth-child(2) > td > a");
//link_text.text();
for (Element l : link_text)
{
String output = l.text();
System.out.println( output );
}
以下内容可以得到表格,但如何向下钻取:
Elements tables = docx.select("table.sparql");
for(Element table : tables)
{
System.out.println(table.toString());
}
答案 0 :(得分:1)
这会完成这项工作吗?
subclasses
答案 1 :(得分:1)
我在http://try.jsoup.org/上尝试了以下CSS Selector查询,它似乎在向我发送文字http://www.wikidata.org/entity/P26c
:
table.sparql > tbody > tr:nth-child(2)
试试这段代码:
Element link_text = document.select("table.sparql > tbody > tr:nth-child(2)");
link_text.getText(); //or I think its text() method
这似乎也很好:
table.sparql > tbody > tr:nth-child(2) > td > a