首页 > 解决方案 > Flutter:从网站获取“原始” html 文档

问题描述

我对(颤振)开发相当陌生,我有一个问题:

这个网站,我想在应用程序中显示内容。据我了解该网站,它是一个服务器站点 html 渲染。由于没有可用于读取数据的 API(至少我没有找到它们),我想获取整个 html 文档并解析所有“有趣”的数据。

您知道如何获取 html 文档以便开始解析吗?还是有更优雅的解决方案来解决我的问题?

信息:我不想制作 html 渲染,我已经构建了自己的 UI,只想插入特定数据

提前非常感谢!

标签: htmlflutter

解决方案


我刚刚http.get在 Flutter 上测试了一个对您指定的 url 的请求并且运行良好。我使用这个包发出 get 请求,我定义了一个异步函数来发出请求,并在mainFlutter 应用程序的函数中调用该函数:

//This import the package
import 'package:http/http.dart' as http;

//...
//Here comes code of Flutter
//...

//Now I define the async function to make the request
void makeRequest() async{
    var response = await http.get('https://speiseplan.app.itelligence.org/');
    //If the http request is successful the statusCode will be 200
    if(response.statusCode == 200){
      String htmlToParse = response.body;
      print(htmlToParse);
    }
}

//...
//Here comes more Flutter code
//...

main(){
    makeRequest();
}

这会将您想要的 html 打印为字符串,现在您可以根据需要对其进行解析。


推荐阅读