コード例 #1
0
        protected List<Flight> ParseFlightTable(HtmlDocument document, string tableId)
        {
            List<Flight> flights = new List<Flight>();

            string xpath = String.Format("//table[@id='{0}']//tr", tableId);

            HtmlNodeCollection rows = document.DocumentNode.SelectNodes(xpath);
            if (null == rows)
            {
                return flights;
            }

            foreach (HtmlNode row in rows)
            {

                HtmlNodeCollection columns = row.SelectNodes("./td");
                if (columns != null && 8 == columns.Count)
                {
                    Flight flight = new Flight();
                    flight.AirlineName = "Mekong Air";

                    HtmlNode flightNoNode = columns[0];
                    HtmlNode linkNode = flightNoNode.SelectSingleNode("./a");
                    if (linkNode != null)
                    {
                        flightNoNode.RemoveChild(flightNoNode.SelectSingleNode("./a"));
                    }

                    string flightNo = flightNoNode.InnerText;
                    flightNo = flightNo.Replace("\r\n ", "");
                    flightNo = flightNo.Replace("  ", "");
                    flightNo = flightNo.Trim();
                    flight.FlightNo = flightNo;

                    int removedLength = 3;
                    if (columns[1] != null)
                    {
                        string text = columns[1].InnerText;
                        flight.StartTime = text.Remove(text.Length - removedLength);
                    }

                    if (columns[2] != null)
                    {
                        string text = columns[2].InnerText;
                        flight.EndTime = text.Remove(text.Length - removedLength);
                    }

                    HtmlNodeCollection pricesNode = row.SelectNodes("./td/p");
                    if (null != pricesNode)
                    {
                        flight.Price = pricesNode.Last().InnerText;
                    }

                    flights.Add(flight);
                }

            }
            return flights;
        }
コード例 #2
0
        protected List<Flight> ParseFlightTable(HtmlDocument document, int index)
        {
            List<Flight> flights = new List<Flight>();

            HtmlNodeCollection tables = document.DocumentNode.SelectNodes("//table[@class='domestic']");
            if (null == tables)
            {
                return flights;
            }

            HtmlNode departTable = tables.ElementAt(index);
            if (null == departTable)
            {
                return flights;
            }

            HtmlNode tableBody = departTable.SelectSingleNode("./tbody");
            if (null == tableBody)
            {
                return flights;
            }

            HtmlNode unnecessaryNode = departTable.SelectSingleNode(".//tr[@class='starter-options ']");
            if (unnecessaryNode != null)
            {
                tableBody.RemoveChild(unnecessaryNode);
            }

            unnecessaryNode = departTable.SelectSingleNode(".//tr[@class='business-options ']");
            if (unnecessaryNode != null)
            {
                tableBody.RemoveChild(unnecessaryNode);
            }

            HtmlNodeCollection rows = tableBody.SelectNodes("./tr");
            if (rows == null)
            {
                return flights;
            }

            foreach (var row in rows)
            {
                Flight flight = new Flight();

                HtmlNode node = row.SelectSingleNode("./td[1]/strong");
                if (node != null)
                {
                    flight.StartTime = node.InnerText;
                }

                node = row.SelectSingleNode("./td[2]/strong");
                if (node != null)
                {
                    flight.EndTime = node.InnerText;
                }

                node = row.SelectSingleNode("./td[4]//label");

                if (node != null)
                {
                    flight.Price = node.InnerText;
                }

                HtmlNode flightNoNode = row.SelectSingleNode(".//span[@class='flight-no']");
                if (flightNoNode != null)
                {
                    unnecessaryNode = flightNoNode.SelectSingleNode("./img");
                    if (unnecessaryNode != null)
                    {
                        flightNoNode.RemoveChild(unnecessaryNode);
                    }
                    flight.FlightNo = flightNoNode.InnerText.Replace("  ", " ");

                    flight.AirlineName = "Jesta";
                    flights.Add(flight);
                }

            }

            return flights;
        }