我查看了JSoup Selector Overview文档,但我仍然遇到问题。我想从网页获取信息,这些信息存储在一个表格中。一旦我检索到这个,我想分成单独的行,以便以后使用它;但我似乎无法做到这一点。
我的JSoup运行正常,但是当我尝试引用我想要的特定表时它不起作用,如:
try
{
Document doc = Jsoup.connect( myHtml ).get();
Elements pTag = doc.select( "table#ctl00_ContentPlaceHolder1_tblPasses" );
//Elements links = doc.select( "a[href]" );
String pTagString = pTag.html();
// Set the textview to the results of the Jsoup query
setData( pTagString );
// Reset msg
msg = null;
msg = handler.obtainMessage( UPDATE_UI );
handler.sendMessage( msg );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
以下是我试图访问的HTML代码段:
<table id="ctl00_ContentPlaceHolder1_tblPasses" class="standardTable" cellspacing="0" cellpadding="4" rules="cols" border="2" style="background-color:White;border-color:Gray;border-width:2px;border-style:Solid;border-collapse:collapse;">
<tr class="tablehead" style="border-width:0px;border-style:None;">
<td valign="middle" rowspan="2">Date</td><td id="ctl00_ContentPlaceHolder1_MagHeader" valign="middle">Brightness</td><td align="center" colspan="3">Start</td><td align="center" colspan="3">Highest point</td><td align="center" colspan="3">End</td>
</tr><tr class="tablehead">
<td id="ctl00_ContentPlaceHolder1_MagHeader2" align="center">(<a id="ctl00_ContentPlaceHolder1_linkMagnitude" title="show definition of this term" href="glossary.aspx?term=magnitude&lat=54.5156&lng=-6.06863&loc=Unspecified&alt=0&tz=GMT">mag</a>)</td><td align="center">Time</td><td><a id="ctl00_ContentPlaceHolder1_linkAltitude" title="show definition of this term" href="glossary.aspx?term=altitude&lat=54.5156&lng=-6.06863&loc=Unspecified&alt=0&tz=GMT">Alt.</a></td><td><a id="ctl00_ContentPlaceHolder1_linkAzimuth" title="show definition of this term" href="glossary.aspx?term=azimuth&lat=54.5156&lng=-6.06863&loc=Unspecified&alt=0&tz=GMT">Az.</a></td><td align="center">Time</td><td>Alt.</td><td>Az.</td><td align="center">Time</td><td>Alt.</td><td>Az.</td>
这只是表格的一部分,还有很多,但是现在我想要进入这个表格并且它是第一行。有什么想法吗?
的更新 的
我试过了:
Elements table = doc.select( "table#ctl00_ContentPlaceHolder1_tblPasses" );
Element first_Row = table.first();
//Elements links = doc.select( "a[href]" );
String first_Row_Strg = first_Row.html().toString();
// Set the textview to the results of the Jsoup query
setData( first_Row_Strg );
但它实际上返回整个表而不是第一行。我应该尝试瞄准表中行的实际标识符吗?如果是这样,你怎么做?
答案 0 :(得分:1)
以不同的方式思考,直接从父母那里选择孩子。
Element first_row = doc.select("table#ctl00_ContentPlaceHolder1_tblPasses tr").first();