Web scrap in Flutter nepalstock.com.np

To scrape lazy-loaded data from the Nepal Stock Exchange website using the dart programming language, you will need to use a package that can execute JavaScript code in order to load the lazy-loaded data. One such package is "dart:js".

Here is an example of how you can use "dart:js" to scrape lazy-loaded data from the Nepal Stock Exchange website:

Copy code
import 'package:html/parser.dart';
import 'package:html/dom.dart';
import 'package:http/http.dart' as http;
import 'package:js/js.dart';

// Function to scrape the lazy-loaded data from the Nepal Stock Exchange website
Future<void> getLazyLoadedData() async {
  // Make a GET request to the website
  var response = await http.get('https://www.nepalstock.com.np/');

  // Parse the HTML content
  var document = parse(response.body);

  // Find the element containing the lazy-loaded data
  var lazyLoadedDataElement = document.getElementById('lazyLoadedData');

  // Check if the element exists
  if (lazyLoadedDataElement != null) {
    // Execute the JavaScript code to load the lazy-loaded data
    context.callMethod(r'$', ['#lazyLoadedData'])
        .callMethod('lazyLoadData');

    // Wait for the lazy-loaded data to be loaded
    await Future.delayed(Duration(seconds: 1));

    // Get the updated HTML content
    response = await http.get('https://www.nepalstock.com.np/');
    document = parse(response.body);

    // Find the updated element containing the lazy-loaded data
    lazyLoadedDataElement = document.getElementById('lazyLoadedData');

    // Print the lazy-loaded data
    print(lazyLoadedDataElement.innerHtml);
  } else {
    print('Lazy-loaded data not found');
  }
}


To scrape the top gainers and losers from the Nepal Stock Exchange website using the dart programming language, you can use the package "html" to parse the HTML content of the website and extract the data you need.

Here is an example of how you can do this:

Copy code
import 'package:html/parser.dart';
import 'package:html/dom.dart';
import 'package:http/http.dart' as http;

// Function to scrape the top gainers and losers from the Nepal Stock Exchange website
Future<void> getTopGainersLosers() async {
  // Make a GET request to the website
  var response = await http.get('https://www.nepalstock.com.np/');

  // Parse the HTML content
  var document = parse(response.body);

  // Find the table containing the top gainers
  var topGainersTable = document.getElementById('topGainers');

  // Get the rows of the table
  var rows = topGainersTable.getElementsByTagName('tr');

  // Print the top gainers
  print('Top gainers:');
  for (var row in rows) {
    var cells = row.getElementsByTagName('td');
    if (cells.length > 1) {
      print(cells[0].text + ': ' + cells[1].text);
    }
  }

  // Find the table containing the top losers
  var topLosersTable = document.getElementById('topLosers');

  // Get the rows of the table
  rows = topLosersTable.getElementsByTagName('tr');

  // Print the top losers
  print('Top losers:');
  for (var row in rows) {
    var cells = row.getElementsByTagName('td');
    if (cells.length > 1) {
      print(cells[0].text + ': ' + cells[1].text);
    }
  }
}


Post a Comment

Previous Post Next Post