Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using HtmlAgilityPack;
namespace AmazonPriceCheck
{
public class AmazonUkStoreParser : IHtmlParser
{
public string GetItemCodeFromUrl(string url)
{
var itemCode = url.Substring(url.IndexOf("/dp/") + 4);
itemCode = itemCode.Substring(0, itemCode.IndexOf("/"));
return itemCode;
}
public decimal GetPriceForUrl(string url)
{
decimal thePrice = -1;
if (string.IsNullOrEmpty(url))
{
return -1;
}
if (!url.Contains(".co.uk"))
{
return -1;
}
if (!url.Contains("amazon.co.uk"))
{
return -1;
}
var pageString = HttpUtils.GetHtmlFromUrl(url);
thePrice = ParseDocumentForPrice(pageString);
return thePrice;
}
private decimal ParseDocumentForPrice(string pageString)
{
var document = new HtmlDocument();
document.LoadHtml(pageString);
var nodes = document.DocumentNode.SelectNodes(
"/html/body/div[@id='a-page'][1]"
+ "/div[@id='dp'][1]"
+ "/div[@id='dp-container'][1]"
+ "/div[@id='centerCol'][1]"
+ "/div[@id='desktop_unifiedPrice'][1]"
+ "/div[@id='unifiedPrice_feature_div'][1]"
+ "/div[@id='price'][1]"
+ "/table"
+ "/tr[@id='priceblock_ourprice_row'][1]"
+ "/td[2]"
+ "/span[@id='priceblock_ourprice']");
var formattedNode = nodes.First().InnerHtml;
formattedNode = formattedNode.Replace(" ", "");
formattedNode = formattedNode.Replace("\n", "");
formattedNode = formattedNode.Replace("£", "");
decimal price = -1;
Decimal.TryParse(formattedNode, out price);
return price;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.