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?Yamin– Yamin2018-08-11 10:26:28 +00:00Commented 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.N. I. Md. Ashafuddula– N. I. Md. Ashafuddula2018-08-11 12:02:50 +00:00Commented Aug 11, 2018 at 12:02
-
Still, use that plugin, use md.Yamin– Yamin2018-08-11 12:24:03 +00:00Commented Aug 11, 2018 at 12:24
Add a comment
|
2 Answers
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.
1 Comment
Mark Carlton
The package in question to be added is -> html: ^0.14.0+4
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
Bonfra
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?