[英]Extract the thread head and thread reply from a forum
我想從論壇中僅提取用戶的視圖和回復以及頭部的標題。 在此代碼中,當您提供url時,代碼將返回所有內容。 我只想要在標題標簽中定義的線程標題和在div內容標簽之間的用戶回復。 幫幫我如何提取。 解釋如何在txt文件中打印它
package extract;
import java.io.*;
import org.jsoup.*;
import org.jsoup.nodes.*;
public class TestJsoup
{
public void SimpleParse()
{
try
{
Document doc = Jsoup.connect("url").get();
doc.body().wrap("<div></div>");
doc.body().wrap("<pre></pre>");
String text = doc.text();
// Converting nbsp entities
text = text.replaceAll("\u00A0", " ");
System.out.print(text);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
TestJsoup tjs = new TestJsoup();
tjs.SimpleParse();
}
}
為什么你將body-Element包裹在div和pre標簽中?
標題元素可以這樣選擇:
Document doc = Jsoup.connect("url").get();
Element titleElement = doc.select("title").first();
String titleText = titleElement.text();
// Or shorter ...
String titleText = doc.select("title").first().text();
DIV-標簽:
// Document 'doc' as above
Elements divTags = doc.select("div");
for( Element element : divTags )
{
// Do something there ... eg. print each element
System.out.println(element);
// Or get the Text of it
String text = element.text();
}
這里是關於整個Jsoup Selector API的概述,這將幫助您找到所需的任何元素。
好吧,我使用了另一個代碼,我收集了這個特定標簽的數據。
Elements content = doc.getElementsByTag(“blockquote”);
元素k = doc.select(“[postcontent restore]”);
。content.select( “BLOCKQUOTE”)除去();
。content.select( “BR”)除去();
。content.select( “DIV”)除去();
。content.select( “a”)的除去();
。content.select( “B”)除去();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.