简体   繁体   中英

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. 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.

code:

translated = elements.html();

Change this line as well from:

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

to

tv_Jsoupe.setText(translated);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM