Aleksandar Jovanovic
Aleksandar Jovanovic

Reputation: 97

Flutter: Get "raw" html document from website

I am fairly new to (flutter) development and I have a question:

There is this website, and I would like to display the content in an app. As far as I understood the website, it is an server-site html render. Since there are no API's (atleast I didn't find them) which I can use to read the data, I wanted to get the whole html document and parse all of the "interesting" data out.

Do you have any idea how I get the html document so that I can start parsing? Or is there a more elegant solution to my problem?

Info: I don't want to make a html render, I have build my own UI and just want to insert specific data

Thanks a lot in advance!

Upvotes: 3

Views: 10591

Answers (2)

Mohammad Abdorrahmani
Mohammad Abdorrahmani

Reputation: 104

In my case, I using the code below, I can receive posts containing HTML tags without any problem.

I also used the flutter_html package to render it.

static Future<List<Article>> fetchArticles({required int page}) async {
    final response =
        await http.get(Uri.parse('https://expample.com/api/posts'));

    if (response.statusCode == 200) {
      final dynamic data = json.decode(response.body);
      print('API Response: $data'); // Print the response for debugging

      if (data is Map<String, dynamic> && data.containsKey('articles')) {
        final List<dynamic> articlesData = data['articles']['data'];

        return articlesData
            .map((article) => Article.fromJson(article))
            .toList();
      } else {
        throw Exception('Invalid API response format');
      }
    } else {
      throw Exception(
          'Failed to load articles. Status Code: ${response.statusCode}');
    }
  }

Upvotes: 0

Ademir Villena Zevallos
Ademir Villena Zevallos

Reputation: 1559

I've just tested an http.get request on Flutter to the url you specified and works well. I used this package to make the get request, I defined an async funcion to make the request and in the main function of a Flutter app I call that function:

//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();
}

This will print the html you want, as String and now you can parse it as you want.

Upvotes: 7

Related Questions