简体   繁体   中英

How can i get a single image from a website using HtmlAgilityPack?

I'm trying to scrape a store using HtmlAgilityPack, i got the product title, price, stock but i'm lacking the image. I searched how to get images using Agility Pack but all that i found is how to get all the images from the website, not just one. I tried selecting the single node where the image is, doing something like this: var imgNode = doc.DocumentNode.SelectSingleNode("//div[@class = 'featured']"); By doing this the imgNode value is equal to the whole img id html element, but i want to get only the current source from the src value. The link from the store i'm trying to scrape is this: "https://www.fullh4rd.com.ar/prod/18381/monitor-27-gigabyte-g27fc-gaming-curvo-ips-165hz-hdmi-dp" Thanks for your time ^^

The src item in an html element, when seen as just an attribute, could be retrieved by it's attributes property.

However the above code selects a div, so once you select the child img element, you can access it's source:

var imgContainer = document.DocumentNode.SelectSingleNode("//div[@class = 'featured']");

var imgNode = imgContainer.SelectSingleNode("//img");

var src = imgNode.Attributes["src"].Value;

Alternatively find the img directly using the id:

var imgContainer = document.DocumentNode.SelectSingleNode("//img[@id = 'mainpic']");
Console.WriteLine(imgContainer.Attributes["src"].Value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM