Josie
Josie

Reputation: 89

Extracting all data from a list <li> tag using HTML Agility Pack in C#

I am using the HTML Agility Pack to extract data. I want to extract all list items from the source:

<div id="feature-bullets" class="a-section a-spacing-medium a-spacing-top-small">

<ul class="a-unordered-list a-vertical a-spacing-mini">

<li><span class="a-list-item">
some data 1

</span></li>

<li><span class="a-list-item">
some data 2

</span></li>

<li><span class="a-list-item">
some data 3

</span></li>

<li><span class="a-list-item">
some data 4

</span></li>

</ul>

My code so far:

string source = someSource

var htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(source);

How can I extract all the list items to get a result similar to this:

List value 1 is: some data 1
List value 2 is: some data 2
List value 3 is: some data 3
List value 4 is: some data 4

Upvotes: 1

Views: 1329

Answers (1)

aepot
aepot

Reputation: 4824

Here is the source that I am using: amazon.co.uk/dp/B07VD9F419. I am trying to extract the data in the bullet points.

Install additional NuGet package Fizzler.Systems.HtmlAgilityPack to enable the QuerySelector feature. The query syntax is the same as in JavaScript.

Consider the following example.

using HtmlAgilityPack;
using Fizzler.Systems.HtmlAgilityPack;
class Program
{
    private static readonly HttpClient client = new HttpClient();

    static async Task Main(string[] args)
    {
        string source = await client.GetStringAsync("https://www.amazon.co.uk/dp/B07VD9F419");

        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(source);

        IEnumerable<HtmlNode> nodes = htmlDoc.DocumentNode.QuerySelectorAll("div#feature-bullets ul li span.a-list-item");

        foreach (HtmlNode node in nodes)
        {
            Console.WriteLine(new string('-', 20) + Environment.NewLine + node.InnerText.Trim());
        }

        Console.ReadKey();
    }
}

Console Output

--------------------
In addition to body weight, it also gives you a realistic picture of your health and fitness with 13 data points, such as body composition, muscle volume etc.
--------------------
High precision With a series of algori thms complexes and advanced bioelectric Impedance Analysis (BIA), provides accurate state dose.
--------------------
Weighs from 100g to 150kg so it can also weigh fruits and vegetables in addition to adults and children.
--------------------
Stores up to 16 profiles

Upvotes: 1

Related Questions