How to extract data from Div Ul Li A tag title value text from Given URL ASP.Net MVC 5 C#
To download the data from the provided URL and extract only the text within the <a>
tags, you can modify the existing code as follows:
Controller code
public ActionResult ExtractFromWiki()
{
string url = "https://en.wikipedia.org/wiki/List_of_American_film_actresses";
WebClient client = new WebClient();
string htmlContent = client.DownloadString(url);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlContent);
// Find all <a> elements within <li> elements
HtmlNodeCollection aNodes = doc.DocumentNode.SelectNodes("//div[@class='div-col']/ul/li/a");
// List to store all titles
List<string> titles = new List<string>();
if (aNodes != null)
{
foreach (HtmlNode aNode in aNodes)
{
// Extract title text
string title = aNode.InnerText;
titles.Add(title.Trim());
}
}
ViewBag.Titles = titles;
return View();
}
View Code
@{
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
</head>
<body>
<div>
<h1>Titles:</h1>
<ul>
@foreach (var title in ViewBag.Titles)
{
<li>@title</li>
}
</ul>
</div>
</body>
</html>
<a>
tags located within <li>
elements inside <ul>
elements with the class "div-col" from the provided URL. Then, it stores the extracted titles in a list and passes it to the view via ViewBag
.
Post a Comment