我有一块HTML,我用Jsoup解析,然而,并不是所有的都是相关的,解析不相关的部分抛出我的数据集。Jsoup开始解析AFTER指定的标签或从页面底部开始?
在该网站上,有一个标题可以随时更改。在这个头里面是链接,但是我不关心的链接。当Jsoup解析文档时,它会将这些文件添加到我的链接数组中,并抛出我的值。
我感兴趣的HTML之后是 <!-- BEGIN TOPICS -->
标记。
我希望能够告诉Jsoup忽略标签上方的所有内容。这可能吗?如果没有,我可以通过在文档底部开始解析来解决这个问题,但我不知道我会如何去解决这个问题。
我的Jsoup查询如下。请忽略所有的注释行和调试语句,我一直在努力工作,这一点了一会儿,仍然在测试代码。
Thread getTitlesThread = new Thread() {
public void run() {
TitleResults titleArray = new TitleResults();
StringBuilder whole = new StringBuilder();
try {
URL url = new URL(
Constants.FORUM);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(new BufferedInputStream(urlConnection.getInputStream())));
String inputLine;
while ((inputLine = in.readLine()) != null)
whole.append(inputLine);
in.close();
} catch (IOException e) {}
finally {
urlConnection.disconnect();
}
} catch (Exception e) {}
Document doc = Parser.parse(whole.toString(), Constants.FORUM);
Elements threads = doc.select("TOPICS > .topic_title");
Elements authors = doc.select("a[hovercard-ref]");
// for (Element author : authors) {
// authorArray.add(author.text());
// }
// cleanAuthors();
if (threads.isEmpty()) {
Log.d("POC", "EMPTY BRO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11");
}
// for (Element thread : threads) {
// titleArray = new TitleResults();
// Log.d("POC", thread.toString());
//
// titleArray.setAuthorDate(authorArray.get(0));
// authorArray.remove(0);
//Thread title
// threadTitle = thread.text();
// titleArray.setItemName(threadTitle);
//
// //Thread link
// String threadStr = thread.attr("abs:href");
// String endTag = "/page__view__getnewpost"; //trim link
// threadStr = new String(threadStr.replace(endTag, ""));
// threadArray.add(threadStr);
// results.add(titleArray);
// }
}
};
getTitlesThread.start();
我能得到它 '书籍DOC = Parser.parse(whole.toString()工作的replaceAll(“<! - 结束广告代码 - > * < - ?。?!BEGIN话题 - >“,”“),Constants.FORUM);' 其中'<! - end ad tag - >'是我想要忽略的开始,'<! - BEGIN TOPICS - > '结束了。 – r2DoesInc 2012-01-05 13:54:00