public static async Task <WalItem> GetDetail(string url) { var item = new WalItem(); string itemNo = null; string descr = null; var images = new List <string>(); try { using (HttpClient client = new HttpClient()) using (HttpResponseMessage response = await client.GetAsync(url)) using (HttpContent content = response.Content) { // ... Read the string. string result = await content.ReadAsStringAsync(); itemNo = parseItemNo(result); item.ItemId = itemNo; if (itemNo == "568336078") { int stop = 1; } string marker = "\"product-short-description-wrapper\" itemprop=\"description\">"; descr = parseDescr(result, marker, "<"); if (string.IsNullOrEmpty(descr)) { marker = "\"product_long_description\":{\"values\":[\""; descr = parseDescr(result, marker, "}"); } item.Description = descr; images = ParseImages(result); if (images.Count == 0) { int stop = 1; } else { item.PictureUrl = dsutil.DSUtil.ListToDelimited(images.ToArray(), ';'); } Console.WriteLine("images: " + images.Count); bool outOfStock = ParseOutOfStock(result); if (outOfStock) { int stop = 1; } item.OutOfStock = outOfStock; } } catch (Exception exc) { string err = exc.Message; } return(item); }
// // get title, price and detail URL // helpful to debugging to log page number public static async Task <List <WalItem> > ProcessTitles(string html, int pageNum, int categoryId) { bool done = false; int pos = 0; int startTitle = 0; int titleLen = 0; int htmlLen = html.Length; int titleCount = 0; var items = new List <WalItem>(); const string titleMarker = "\"productType\":\"REGULAR\",\"title\":"; string productPageUrl = null; string offerPrice = null; const string whereToStart = "Walmart mobile apps"; pos = html.IndexOf(whereToStart); try { do { pos = html.IndexOf(titleMarker, pos); if (pos == -1) { done = true; } else { int endPos = html.IndexOf("\",\"", pos + titleMarker.Length + 1); startTitle = pos + titleMarker.Length + 1; titleLen = endPos - pos - titleMarker.Length - 1; string title = html.Substring(startTitle, titleLen); Console.WriteLine(title); dsutil.DSUtil.WriteFile(Log_File, title); productPageUrl = "https://www.walmart.com" + getProductPageUrl(html, endPos); dsutil.DSUtil.WriteFile(Log_File, productPageUrl); offerPrice = getOfferPrice(html, endPos); dsutil.DSUtil.WriteFile(Log_File, "offer price: " + offerPrice); dsutil.DSUtil.WriteFile(Log_File, "page num: " + pageNum); dsutil.DSUtil.WriteFile(Log_File, "", blankLine: true); var item = new WalItem(); item.DetailUrl = productPageUrl; item.Title = title; item.CategoryID = categoryId; item.Price = Convert.ToDecimal(offerPrice); //item.DetailUrl = "https://www.walmart.com/ip/RustOleum-Protective-Enamel-Oil-Based-Gloss-Sunrise-Red/21798791"; var detail = await GetDetail(item.DetailUrl); item.ItemId = detail.ItemId; item.PictureUrl = detail.PictureUrl; item.Description = detail.Description; item.OutOfStock = detail.OutOfStock; items.Add(item); ++titleCount; pos += titleMarker.Length; } } while (!done); } catch (Exception exc) { string err = exc.Message; } return(items); }