HTML parsing in FLUTTER for android / iOS development

℡╲_俬逩灬. 提交于 2020-12-30 08:44:05

问题


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.


回答1:


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.




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


来源:https://stackoverflow.com/questions/51798706/html-parsing-in-flutter-for-android-ios-development

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!