6

We know there is a Jsoup library for android developers to parse html text, code etc. As I am new in flutter mobile app development I want to know if there is any library like Jsoup to parse html text,code from a web site in flutter.

3
  • It depends on your purpose. if you just want to display a webpage, use this plugin pub.dartlang.org/packages/flutter_webview_plugin . what is your use case which this plugin cannot do? Commented Aug 11, 2018 at 10:26
  • No, actually I want to display only a few text or content like a headline of a news from a newspaper website, not the full webpage. Commented Aug 11, 2018 at 12:02
  • Still, use that plugin, use md. Commented Aug 11, 2018 at 12:24

2 Answers 2

12

You can parse a HTML string this way

import ‘package:html/parser.dart’;
//here goes the function 

String _parseHtmlString(String htmlString) {

var document = parse(htmlString);

String parsedString = parse(document.body.text).documentElement.text;

return parsedString;
}

Please let me know this doesn’t solve your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

The package in question to be added is -> html: ^0.14.0+4
2

I solve the problem for me. Firstly, to user parser you should fetch data using http.get(url). After that you can parse what you want.

Fetch html page:

Future<String> fetchHTML(String url) async {
  final response = await http.get(url);

  if (response.statusCode == 200)
    return response.body;
  else throw Exception('Failed');
}

After that you must call FutureBuilder()

    FutureBuilder<String>(
      future: fetchHTML('http://your_page.ru/page.html'),
      builder: (context, snapshot){
        if (snapshot.hasData) {
          //Your downloaded page
          _temp = snapshot.data;
          print(snapshot.data);
          return Text('Finished');
        }
        else if (snapshot.hasError)
          return Text('ERROR');

        return Text('LOADING');
      },
    ),

And now you can parse it:

parse(_temp);

1 Comment

hey i'm in your exact situation but the get functions throw me an error that i think is related to the request header but don't know how to fix. any idea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.