C# HTML Agility Pack SelectSingleNode and SelectNodes XPath syntax -
my question similar 1 xmlnode.selectsinglenode syntax search within node in c#
i'm trying use html agility pack pull price/condition/ship price... here's url scraping: http://www.amazon.com/gp/offer-listing/0470108541/ref=dp_olp_used?ie=utf8&condition=all
here's snippet of code:
string results = ""; var w = new htmlweb(); var doc = w.load(url); var nodes = doc.documentnode.selectnodes("//div[@class='a-row a-spacing-medium olpoffer']"); if (nodes != null) { foreach (htmlnode item in nodes) { var price = item.selectsinglenode(".//span[@class='a-size-large a-color-price olpofferprice a-text-bold']").innertext; var condition = item.selectsinglenode(".//h3[@class='a-spacing-small olpcondition']").innertext; var price_shipping = item.selectsinglenode("//span[@class='olpshippingprice']").innertext; results += "price " + price + " condition " + condition + " ship " + price_shipping + "\r\n"; } } return results;
no matter combination try of .// , . , ./ , / etc... cannot want (just trying learn xpaths), returning 1st item on , on , over, original question referenced earlier. think i'm missing fundamental understanding of how selecting nodes work and/or considered node.
update
ok, i've changed url point different book , first 2 items working expected... when try change third item (price_shipping) ".//" absolutely no information being pulled anything. must due sometime there not shipping price , span omitted. how handle this? tried if price_shipping !=null.
update
solved. removed ".innertext" price_shipping causing issues when null... did null check , safe use .innertext.
solved. removed ".innertext" price_shipping causing issues when null... did null check , safe use .innertext.
Comments
Post a Comment