问题
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