Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Web scraping with flutter

What is web scraping?

So, In easy words, web scraping means fetch data from website. So you can easily fetch any website data and show in your flutter or any app. In this post, I will tell you, how you can fetch links, innerhtml and ,

data. So by understanding this post, You can easily Fetch data from website. So you can also able to fetch picture from website by reading this post.

So in this post, I fetch title , link and code from website and show in listview. And I apply some animation in listview to also include dependency for animation.

So Let's Start Code:
Step 1:

Include this dependency to pubspec.yaml file.

html: ^0.14.0+3
http: ^0.12.0+4
flutter_staggered_animations: "^0.1.2"
url_launcher: ^5.4.0

Step 2:

Create a new dart file and create a class in it and give name as MyHomePage and paste code in it.

import 'package:flutter/material.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart' as parser;
import 'package:html/dom.dart' as dom;
import 'package:url_launcher/url_launcher.dart';

class MyHomepage extends StatefulWidget {
  @override
  _MyHomepageState createState() => _MyHomepageState();
}

class _MyHomepageState extends State {
  List title = List();
  List post = List();
  List link = List();
  void _getData() async {
    final response = await http.get('https://arprogramming.blogspot.com/');
    dom.Document document = parser.parse(response.body);
    final elements = document.getElementsByClassName('entry-header blog-entry-header');
    final element2= document.getElementsByClassName('entry-content');
    final linkElemnt=document.getElementsByClassName('entry-title');
    setState(() {
      title = elements
          .map((element) =>
      element.getElementsByTagName("a")[0].innerHtml)
          .toList();
      post = element2
          .map((element) =>
      element.getElementsByTagName("p")[0].innerHtml)
          .toList();
      link = linkElemnt
          .map((element) =>
      element.getElementsByTagName("a")[0].attributes['href'])
          .toList();
    });
  }


  @override
  // ignore: must_call_super
  void initState() {
    _getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.black87,
        body: post.length == 0 ? Text("zeroo"): ListView.builder(
          itemCount: post.length,
          itemBuilder: (context, index) {
            return AnimationConfiguration.staggeredList(
                position: index,
                duration: const Duration(milliseconds: 375),
              child: SlideAnimation(
              verticalOffset: 50.0,
              child: FadeInAnimation(
                child: GestureDetector(
                  onTap: () async{
                    dynamic url=link[index];
                    if (await canLaunch(url))
                      launch(url);
                    else
                    {
                      print('error');
                    }

                  },
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Card(
                      child: Container(
                        color: Colors.black87,
                        child: Column(
                          children: [
                            Align(
                              alignment: Alignment.centerLeft,
                              child: Text(title[index],
                                style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  color: Colors.red,
                                  fontSize: 20,
                                ),
                              ),
                            ),
                            SizedBox(height: 15),
                            Text(
                              post[index],
                              style: TextStyle(
                                color: Colors.white,
                              ),
                            )
                          ],
                        ),

                      ),
                    ),
                  ),
                ),
              ),
              ),
            );
          },
        ));
  }
}

Step 3:

Paste code to main.dart file.

import 'package:flutter/material.dart';
void main()=> runApp(MyApp());
class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primaryColor: Colors.green,
      ),
      home: MyHomepage (),
    );
  }
}

How this code works?
So In this code, Main.dart file calls the MyHomePage class and then in  this class, I create 3 array and store website data in it. and after that I show data in listview.

OUTPUT:


Want To watch Video?



SO,

Hello EveryOne, Here is Ammara Rasheed, your Guider. If you need any help feel free to contact me in this email, also if you find any mistake in code, let’s discuss.


[email protected]



This post first appeared on AR Programming, please read the originial post: here

Share the post

Web scraping with flutter

×

Subscribe to Ar Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×