简体   繁体   English

使用 Jsoup 抓取网站元素时保留原始 Html 格式

[英]Keep the original Html format when scrapping a website element using Jsoup

The below code only retrieves the simple text but I want to retain the HTML format.下面的代码只检索简单的文本,但我想保留 HTML 格式。 Here is my sample code:这是我的示例代码:

public class doIT extends AsyncTask<Void,Void,Void> {
    //String words = wordToTranslate;
    String translated;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        
    }
    @Override
    protected Void doInBackground(Void... params) {
        try
        {
            Document document = Jsoup.connect("https://studentdevos.com/").get();
            org.jsoup.select.Elements elements = document.getElementsByClass("post-entry");

            translated = Jsoup.parse(elements.html()).wholeText();
            
        }
        catch (IOException e)
        {
            e.printStackTrace();
        } return null;
    }
    @Override
    protected void onPostExecute(Void aVoid)
    {

        tv_Jsoupe.setText(Html.fromHtml(translated));
        super.onPostExecute(aVoid);

    }
}

I think that the Jsoup.parse() is what retrieves the simple text.我认为 Jsoup.parse() 是检索简单文本的方法。

code:代码:

translated = elements.html();

Change this line as well from:也从以下位置更改此行:

tv_Jsoupe.setText(Html.fromHtml(translated));

to

tv_Jsoupe.setText(translated);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用jsoup从网站获取所有html - Not getting all html from website using jsoup 使用Jsoup解析时,将HTML布尔属性保留为其原始形式 - Keeping HTML boolean attributes in their original form when parsing with Jsoup 使用Jsoup清理html时,保持属性具有特定值 - Keep attributes with certain value when cleaning html with Jsoup 使用Jsoup库从android网站上获取html表数据, - Get data of html table from a website in android using Jsoup library, 如何使用jquery将段落的原始文本格式适合html段落元素? - how to fit the original text format of a paragraph into the html paragraph element using jquery? 使用 CDKdraganddrop 将容器的一个元素拖动到另一个元素时,是否可以保留原始容器的样式 - Is it possible to keep the styling of the original container when dragging one element of a container to another using CDKdraganddrop 为什么Jsoup在解析HTML元素时会移动它? - Why did Jsoup move an HTML element when it was parsed? 使用Jsoup从HTML页面中的元素中提取文本 - Extracting texts from an element in a HTML page using Jsoup 使用Jsoup从HTML解析数据会导致“span”元素出现问题 - Parsing data from HTML using Jsoup causing trouble with “span” element 使用Jsoup从android应用程序的html文档中提取元素 - Extracting Element from html document for android app using Jsoup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM