How to Extract Data From Multiple Div Ul Li A Tag Title Value Text From Given URL ASP.Net MVC 5 C#
you can achieve this by using the HtmlAgilityPack library to parse the HTML and extract the required data. First, you need to install the HtmlAgilityPack NuGet package in your ASP.NET MVC project. Then, you can create a method to parse the HTML and extract the desired information. Here's how you can do it:
Controller Code:
Install-Package HtmlAgilityPack
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace YourNamespace.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
// Load the HTML content from the URL
string url = "https://en.wikipedia.org/wiki/List_of_actors_with_Hollywood_Walk_of_Fame_motion_picture_stars";
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(url);
// Extract the desired data
List<Tuple<string, string>> actorData = new List<Tuple<string, string>>();
var actorNodes = doc.DocumentNode.SelectNodes("//div[@class='div-col']/ul/li");
if (actorNodes != null)
{
foreach (var node in actorNodes)
{
var actorNameNode = node.SelectSingleNode("a");
var ageNode = node.SelectSingleNode("span[@class='noprint ForceAgeToShow']");
if (actorNameNode != null && ageNode != null)
{
string actorName = actorNameNode.InnerText.Trim();
string age = ageNode.InnerText.Trim();
actorData.Add(new Tuple<string, string>(actorName, age));
}
}
}
// Pass the extracted data to the view
return View(actorData);
}
}
}
This code will fetch the HTML content from the provided URL, parse it using HtmlAgilityPack, extract the actor names and ages, and then display them in a table on the view.
Post a Comment