Home > Mobile >  Keep the original Html format when scrapping a website element using Jsoup
Keep the original Html format when scrapping a website element using Jsoup

Time:01-28

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);

    }
}

CodePudding user response:

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);
  • Related