andruhovski icon

Aspose Demo 02

andruhovski | PRO | 08/21/17 09:40:15 AM UTC | 0 ⭐ | 702 👁️ | Never ⏰ | []
C# |

4.69 KB

|

None

|

0 👍

/

0 👎

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Aspose.Html;
 
namespace AspDemo02
{
    class Program
    {
        static void Main()
        {
            const string inputHtml = "http://www.motorcycle-usa.com/street-bike-buyers-guide/";
            const string outputHtml = @"C:\asposedemo\files\bikes.html";
            var bikes = RetrieveBikes(inputHtml);
            GenerateBikesDocs(outputHtml, bikes);
        }
 
        private static List<StreetBike> RetrieveBikes(string url)
        {
            List<StreetBike> bikes = new List<StreetBike>();
            Console.WriteLine("Loading...");
            Aspose.Html.HTMLDocument document;
            try
            {
                document = new Aspose.Html.HTMLDocument(new Aspose.Html.Url(url));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
                return null;
            }
            Console.WriteLine("Processing...");
            var details = document.QuerySelectorAll("div.results-display");
            var images = document.QuerySelectorAll("div.results-display2");
            for (int i = 0; i < details.Length; i++)
            {
                bikes.Add(new StreetBike
                {
                    Year = uint.Parse(details[i].QuerySelector("div.year").TextContent),
                    Brand = details[i].QuerySelector("div.make").TextContent,
                    Model = details[i].QuerySelector("div.model").TextContent,
                    ImageLink = images[i].QuerySelector("div.image").ParentElement.GetElementsByTagName("img").First().GetAttribute("src")
                });
            };
            return bikes;
        }
 
        private static void GenerateBikesDocs(string outputHtml, List<StreetBike> bikes)
        {
            var document = new Aspose.Html.HTMLDocument();
            Console.WriteLine("Generating...");
            HTMLTableElement table = document.CreateElement("table") as HTMLTableElement;
            HTMLTableSectionElement tHead = document.CreateElement("thead") as HTMLTableSectionElement;
            HTMLTableRowElement headRow = document.CreateElement("tr") as HTMLTableRowElement;
            HTMLTableCellElement headYear = document.CreateElement("th") as HTMLTableCellElement;
            headYear.TextContent = "Year";
            HTMLTableCellElement headBrand = document.CreateElement("th") as HTMLTableCellElement;
            headBrand.TextContent = "Brand";
            HTMLTableCellElement headModel = document.CreateElement("th") as HTMLTableCellElement;
            headModel.TextContent = "Model";
            HTMLTableCellElement headImage = document.CreateElement("th") as HTMLTableCellElement;
            headImage.TextContent = "Image";
            headRow.AppendChild(headYear);
            headRow.AppendChild(headBrand);
            headRow.AppendChild(headModel);
            headRow.AppendChild(headImage);
            tHead.AppendChild(headRow);
 
            HTMLTableSectionElement tBody = document.CreateElement("tbody") as HTMLTableSectionElement;
            foreach (var streetBike in bikes)
            {
                HTMLTableRowElement row = document.CreateElement("tr") as HTMLTableRowElement;
                HTMLTableCellElement cellYear = document.CreateElement("td") as HTMLTableCellElement;
                HTMLTableCellElement cellBrand = document.CreateElement("td") as HTMLTableCellElement;
                HTMLTableCellElement cellModel = document.CreateElement("td") as HTMLTableCellElement;
                HTMLTableCellElement cellImage = document.CreateElement("td") as HTMLTableCellElement;
                HTMLImageElement imgElement = document.CreateElement("img") as HTMLImageElement;
                cellYear.TextContent = streetBike.Year.ToString();
                cellBrand.TextContent = streetBike.Brand;
                cellModel.TextContent = streetBike.Model;
                row.AppendChild(cellYear);
                row.AppendChild(cellBrand);
                row.AppendChild(cellModel);
                imgElement.Src = streetBike.ImageLink;
                imgElement.Width = 320;
                imgElement.Width = 240;
                imgElement.Alt = $"{streetBike.Brand} {streetBike.Model} ({streetBike.Year})";
                cellImage.AppendChild(imgElement);
                row.AppendChild(cellImage);
                tBody.AppendChild(row);
            }
            table.AppendChild(tHead);
            table.AppendChild(tBody);
            document.Body.AppendChild(table);
            document.Save(outputHtml);
        }
    }
}

Comments