Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6805875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:38:26+00:00 2026-05-26T19:38:26+00:00

I am trying to use the HTML Agility pack to scrape some data from

  • 0

I am trying to use the HTML Agility pack to scrape some data from a site. I am really struggling in figuring out how to use selectnodes inside a foreach and then exporting the data to a list or array.

Here is the code I am working with so far.

       string result = string.Empty;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(http://www.amazon.com/gp/offer-listing/B002UYSHMM/);
        request.Method = "GET";

        using (var stream = request.GetResponse().GetResponseStream())
        using (var reader = new StreamReader(stream, Encoding.UTF8))
        {
            result = reader.ReadToEnd();
        }

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.Load(new StringReader(result));
        HtmlNode root = doc.DocumentNode;

        string itemdesc = doc.DocumentNode.SelectSingleNode("//h1[@class='producttitle']").InnerText;  //this works perfectly to get the title of the item
        //HtmlNodeCollection sellers = doc.DocumentNode.SelectNodes("//id['bucketnew']/div/table/tbody/tr/td/ul/a/img/@alt");//this does not work at all in getting the alt attribute from the seller images
        HtmlNodeCollection prices = doc.DocumentNode.SelectNodes("//span[@class='price']"); //this works fine getting the prices
        HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='resultsset']/table/tbody[@class='result']/tr"); //this is the code I am working on to try to collect each tr in the result.  I then want to eather add each span.price to a list from this and also add each alt attribute from the seller image to a list.  Once I get this working I will want to use an if statement in the case that there is text for the seller name instead of an image.

        List<string> sellers = new List<string>();
        List<string> prices = new List<string>();

        foreach (HtmlNode node in nodes)
        {
            HtmlNode seller = node.SelectSingleNode(".//img/@alt");  // I am not sure if this works
            sellers.Add(seller.SelectSingleNode("img").Attributes["alt"]); //this definitly does not work and will not compile.

        }

I have comments in the code above showing what works and what doesn’t and sort of what I want to accomplish.

If anyone has any sugguestions or reading that would be great! I have been searching forums and examples and have not come accross anything that I can use.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:38:27+00:00Added an answer on May 26, 2026 at 7:38 pm

    Your first problem with the commented out SelectNodes doesn’t work because ‘id’ is not an element name, it’s an attribute name. You’ve used the correct syntax in your other expressions for selecting an attribute and comparing the value. Eg, //ElementName[@attributeName='value']. I think even [attributeName='value'] should work, but I have not tested this.

    The syntax inside the SelectNodes function is called “XPath”. This link might help you out.

    The seller node you are selecting is a sibling of node for the current iteration that is an img with an alt attribute. However I think the correct syntax you want is just img[@alt].

    The next problem where you say it won’t compile, check the error message, it will probably be complaining back argument types. sellers.Add I think is looking to name another HtmlNode, not an attribute which is what the expression inside the add is returning.

    Also, check out the Html Agility pack docs and other questions regarding syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use XPath to extract some HTML tags and data and
I'm trying to use WWW::Mechanize to extract some links from the HTML page using
I have been trying to use the HTML Agility Pack to parse HTML into
I'm trying to use Ajax to fetch an HTML page and then pull out
I am trying to use load() to place some html into a div on
I'm trying to use Qt to download the html code from the following url:
I'm trying to use the WebClient class to download a html file from another
I'm trying to use html and css to present some simple vector notation in
We're trying to use HTML code in some category names, i.e. to show quarter
I am trying to use the country detection by IP, an API from http://www.hostip.info/use.html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.