【问题标题】:Extract the thread head and thread reply from a forum从论坛中提取主题头和主题回复
【发布时间】:2012-10-22 06:14:50
【问题描述】:

我只想从论坛中提取用户的意见和回复以及负责人的标题。在此代码中,当您提供 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();

    }

}

【问题讨论】:

    标签: html extract jsoup forum


    【解决方案1】:

    为什么要将 body-Element 包裹在 div 和 pre Tag 中?

    标题元素可以这样选择:

    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 的概述,这将帮助您找到所需的任何类型的元素。

    【讨论】:

    • 这解决了您的问题还是 div-select 超出了需要?
    【解决方案2】:

    好吧,我使用了另一个代码,并从这个特定的标签中收集了数据。

    元素内容 = doc.getElementsByTag("blockquote");

    元素 k=doc.select("[postcontent restore]");

    content.select("blockquote").remove();

    content.select("br").remove();

    content.select("div").remove();

    content.select("a").remove();

    content.select("b").remove();

    【讨论】:

    • 但是我得到这条线有什么问题,我不知道如何删除这个
    猜你喜欢
    • 1970-01-01
    • 2010-11-05
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    相关资源
    最近更新 更多