Advertisement
andruhovski

Aspose Demo 02

Aug 21st, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using Aspose.Html;
  6.  
  7. namespace AspDemo02
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             const string inputHtml = "http://www.motorcycle-usa.com/street-bike-buyers-guide/";
  14.             const string outputHtml = @"C:\asposedemo\files\bikes.html";
  15.             var bikes = RetrieveBikes(inputHtml);
  16.             GenerateBikesDocs(outputHtml, bikes);
  17.         }
  18.  
  19.         private static List<StreetBike> RetrieveBikes(string url)
  20.         {
  21.             List<StreetBike> bikes = new List<StreetBike>();
  22.             Console.WriteLine("Loading...");
  23.             Aspose.Html.HTMLDocument document;
  24.             try
  25.             {
  26.                 document = new Aspose.Html.HTMLDocument(new Aspose.Html.Url(url));
  27.             }
  28.             catch (Exception ex)
  29.             {
  30.                 Console.WriteLine("Error: {0}", ex.Message);
  31.                 return null;
  32.             }
  33.             Console.WriteLine("Processing...");
  34.             var details = document.QuerySelectorAll("div.results-display");
  35.             var images = document.QuerySelectorAll("div.results-display2");
  36.             for (int i = 0; i < details.Length; i++)
  37.             {
  38.                 bikes.Add(new StreetBike
  39.                 {
  40.                     Year = uint.Parse(details[i].QuerySelector("div.year").TextContent),
  41.                     Brand = details[i].QuerySelector("div.make").TextContent,
  42.                     Model = details[i].QuerySelector("div.model").TextContent,
  43.                     ImageLink = images[i].QuerySelector("div.image").ParentElement.GetElementsByTagName("img").First().GetAttribute("src")
  44.                 });
  45.             };
  46.             return bikes;
  47.         }
  48.  
  49.         private static void GenerateBikesDocs(string outputHtml, List<StreetBike> bikes)
  50.         {
  51.             var document = new Aspose.Html.HTMLDocument();
  52.             Console.WriteLine("Generating...");
  53.             HTMLTableElement table = document.CreateElement("table") as HTMLTableElement;
  54.             HTMLTableSectionElement tHead = document.CreateElement("thead") as HTMLTableSectionElement;
  55.             HTMLTableRowElement headRow = document.CreateElement("tr") as HTMLTableRowElement;
  56.             HTMLTableCellElement headYear = document.CreateElement("th") as HTMLTableCellElement;
  57.             headYear.TextContent = "Year";
  58.             HTMLTableCellElement headBrand = document.CreateElement("th") as HTMLTableCellElement;
  59.             headBrand.TextContent = "Brand";
  60.             HTMLTableCellElement headModel = document.CreateElement("th") as HTMLTableCellElement;
  61.             headModel.TextContent = "Model";
  62.             HTMLTableCellElement headImage = document.CreateElement("th") as HTMLTableCellElement;
  63.             headImage.TextContent = "Image";
  64.             headRow.AppendChild(headYear);
  65.             headRow.AppendChild(headBrand);
  66.             headRow.AppendChild(headModel);
  67.             headRow.AppendChild(headImage);
  68.             tHead.AppendChild(headRow);
  69.  
  70.             HTMLTableSectionElement tBody = document.CreateElement("tbody") as HTMLTableSectionElement;
  71.             foreach (var streetBike in bikes)
  72.             {
  73.                 HTMLTableRowElement row = document.CreateElement("tr") as HTMLTableRowElement;
  74.                 HTMLTableCellElement cellYear = document.CreateElement("td") as HTMLTableCellElement;
  75.                 HTMLTableCellElement cellBrand = document.CreateElement("td") as HTMLTableCellElement;
  76.                 HTMLTableCellElement cellModel = document.CreateElement("td") as HTMLTableCellElement;
  77.                 HTMLTableCellElement cellImage = document.CreateElement("td") as HTMLTableCellElement;
  78.                 HTMLImageElement imgElement = document.CreateElement("img") as HTMLImageElement;
  79.                 cellYear.TextContent = streetBike.Year.ToString();
  80.                 cellBrand.TextContent = streetBike.Brand;
  81.                 cellModel.TextContent = streetBike.Model;
  82.                 row.AppendChild(cellYear);
  83.                 row.AppendChild(cellBrand);
  84.                 row.AppendChild(cellModel);
  85.                 imgElement.Src = streetBike.ImageLink;
  86.                 imgElement.Width = 320;
  87.                 imgElement.Width = 240;
  88.                 imgElement.Alt = $"{streetBike.Brand} {streetBike.Model} ({streetBike.Year})";
  89.                 cellImage.AppendChild(imgElement);
  90.                 row.AppendChild(cellImage);
  91.                 tBody.AppendChild(row);
  92.             }
  93.             table.AppendChild(tHead);
  94.             table.AppendChild(tBody);
  95.             document.Body.AppendChild(table);
  96.             document.Save(outputHtml);
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement