'Flutter(Dart) , webscraper plugin gives error on different url's

 _getData() async {
    webScraper = WebScraper('https://www.yesilyurtgame.com');
    print("İm waiting");

    if (await webScraper.loadWebPage('/steamko')) {
      print("İm got in");
      List<Map<String, dynamic>> results =
      webScraper.getElement('div.center', ['title']);
      setState(() {
        loaded = true;
        popNum = results[0]['title'];
      });
    }
  }

There's my code for scraping some websites with a web scraper. I get

Restarted application in 269ms.
İm waiting
Error: Instance of 'WebScraperException'
    at Object.throw_ [as throw] (http://localhost:55475/dart_sdk.js:4328:11)
    at web_scraper.WebScraper.new.loadWebPage (http://localhost:55475/packages/web_scraper/web_scraper.dart.lib.js:68:23)
    at loadWebPage.throw (<anonymous>)
    at http://localhost:55475/dart_sdk.js:37599:38
    at _RootZone.runBinary (http://localhost:55475/dart_sdk.js:37452:58)
    at _FutureListener.thenAwait.handleError (http://localhost:55475/dart_sdk.js:32436:48)
    at handleError (http://localhost:55475/dart_sdk.js:32987:51)
    at Function._propagateToListeners (http://localhost:55475/dart_sdk.js:33013:17)
    at _Future.new.[_completeError] (http://localhost:55475/dart_sdk.js:32860:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:55475/dart_sdk.js:32898:31)
    at Object._microtaskLoop (http://localhost:55475/dart_sdk.js:37708:13)
    at _startMicrotaskLoop (http://localhost:55475/dart_sdk.js:37714:13)
    at http://localhost:55475/dart_sdk.js:33226:9
Application finished.

But when I try to scrape for example:


  _getData() async {
    webScraper = WebScraper('https://worldpopulationreview.com');
    if (await webScraper.loadWebPage('/')) {
      List<Map<String, dynamic>> results =
      webScraper.getElement('div.center', ['title']);
      setState(() {
        loaded = true;
        popNum = results[0]['title'];
      });
    }
  }

This works. I tried out some other URL's too but I only could find this link(that I got from the tutorial) works. I need to scrape a different variety of sites so, I want to know if there's a better way to scrape or make this plugin work?



Solution 1:[1]

Your source page does not contain any div with center class. So it will be null. You should consider the elements while scraping a web page. In your source, there is a web element below:

       <h1 class="gfont">Knight Online (SteamKO) GB</h1>

This is a h1 element and its class is gfont. Then you can get that element with this code:

  _getData() async {
      webScraper = WebScraper('https://worldpopulationreview.com');
      if (await webScraper.loadWebPage('/')) {
        List<Map<String, dynamic>> results =
        webScraper.getElement('h1.gfont', []);
        setState(() {
          loaded = true;
          popNum = results[0];
        });
      }
    }
   

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Akif