Esempio n. 1
0
        private List<Flight> QUNAR_Get(City fromCity, City toCity, string departDate)
        {
            List<Flight> lstFlight = new List<Flight>();
            DateTime dtDepart = DateTime.Parse(departDate);
            //http://flight.qunar.com/site/oneway_list.htm?searchDepartureAirport=%E5%B9%BF%E5%B7%9E&searchArrivalAirport=%E5%8C%97%E4%BA%AC&searchDepartureTime=2015-11-03&searchArrivalTime=2015-11-03&nextNDays=0&startSearch=true&fromCode=CAN&toCode=BJS&from=qunarindex&lowestPrice=null

            string strUrl = string.Format("http://www.ceair.com/flight2014/{0}-{1}-{2}_CNY.html", fromCity, toCity, dtDepart.ToString("yyMMdd"));
            try
            {
                Thread thread = new Thread(delegate()
                {
                    var p = new PageSnatchV3();
                    //p.Timeout = 20000;
                    //p.Url = strUrl;
                    ////p.SnatchCompleted += new SnatchCompletedEventHandler(p_SnatchCompleted);
                    //p.SnatchCompleted += delegate(object sender, SnatchCompletedEventArgs e)
                    //        {
                    //            if (e.Error != null)
                    //                System.Diagnostics.Debug.Write(e.Error);
                    //            else
                    //            {
                    //                System.Diagnostics.Debug.Write(e.Text);
                    //                System.Diagnostics.Debug.Write("=".PadLeft(50, '='));
                    //                System.Diagnostics.Debug.Write(e.TextAsync);
                    //            }
                    //        };
                    System.Diagnostics.Debug.Write(p.Navigate(strUrl, 200));
                });

                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
            }
            catch (Exception ex) { throw ex; }

            return lstFlight;
        }
Esempio n. 2
0
        private List<Flight> CEAIR_Get(City fromCity, City toCity, string departDate)
        {
            List<Flight> lstFlight = new List<Flight>();
            DateTime dtDepart = DateTime.Parse(departDate);
            string strUrl = string.Format("http://www.ceair.com/flight2014/{0}-{1}-{2}_CNY.html", fromCity.C_CE_CODE, toCity.C_CE_CODE, dtDepart.ToString("yyMMdd"));
            string strHTML = string.Empty;
            try
            {
                Thread thread = new Thread(delegate()
                {
                    var p = new PageSnatchV3();
                    strHTML = p.Navigate(strUrl, 200);
                });
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
            }
            catch (Exception ex) { throw ex; }
            string path1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "article.txt");
            //string downloadStr = File.ReadAllText(path1, System.Text.Encoding.GetEncoding("GB2312"));
            string downloadStr = File.ReadAllText(path1, System.Text.Encoding.UTF8);
            HtmlDocument htmlPage = new HtmlDocument();
            htmlPage.LoadHtml(downloadStr);
            HtmlNode docNode = htmlPage.DocumentNode;

            int articleIndex = 1;
            foreach (HtmlNode childNode in docNode.ChildNodes)
            {
                System.Diagnostics.Debug.WriteLine(childNode.Name);
                if (childNode.Name.Equals("article", StringComparison.CurrentCultureIgnoreCase))
                {
                    string xpathPrefix = string.Format("/article[{0}]/ul/li", articleIndex);
                    Flight f = new Flight();
                    f.C_DateSource = "CE AIR";
                    f.C_From = fromCity.C_NAME;
                    f.C_To = toCity.C_NAME;
                    f.C_Departure = departDate;
                    string flightNo = this.GetInnerText(childNode.SelectSingleNode(xpathPrefix + "[@class='f-i']").ChildNodes[1]);
                    string[] flightInfo = flightNo.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    if (flightInfo.Length >= 2)
                    {
                        f.C_Airline = flightInfo[0];
                        f.C_FlightNo = flightInfo[1];
                    }
                    else
                    {
                        f.C_FlightNo = flightNo;
                    }

                    f.C_DEPTIME = this.GetInnerText(childNode.SelectSingleNode(xpathPrefix + "[@class='f-i']/div[@class='info clearfix']/div[@class='airport r']").ChildNodes[0]);
                    string depart = this.GetInnerText(childNode.SelectSingleNode(xpathPrefix + "[@class='f-i']/div[@class='info clearfix']/div[@class='airport r']"));
                    f.C_ARRTIME = this.GetInnerText(childNode.SelectSingleNode(xpathPrefix + "[@class='f-i']/div[@class='info clearfix']/div[@class='airport']").ChildNodes[0]);
                    string arrive = this.GetInnerText(childNode.SelectSingleNode(xpathPrefix + "[@class='f-i']/div[@class='info clearfix']/div[@class='airport']"));
                    f.C_TotalTime = this.GetInnerText(childNode.SelectSingleNode(xpathPrefix + "[@class='f-i']/dfn"));
                    decimal outPrice;
                    string strFirstPrice = this.GetInnerText(childNode.SelectSingleNode(xpathPrefix + "[@name='fb']")).Replace("¥", string.Empty);
                    if (decimal.TryParse(strFirstPrice, out outPrice))
                        f.C_FirstClass = outPrice;

                    string strEconomyPrice = this.GetInnerText(childNode.SelectSingleNode(xpathPrefix + "[@name='economy']")).Replace("¥", string.Empty);
                    if (decimal.TryParse(strEconomyPrice, out outPrice))
                        f.C_Economy = outPrice;

                    string strPrice = this.GetInnerText(childNode.SelectSingleNode(xpathPrefix + "[@name='more']")).Replace("¥", string.Empty);
                    StringBuilder sbPriceInfo = new StringBuilder();
                    sbPriceInfo.AppendFormat("超值特惠:{0};", strPrice);
                    if (childNode.SelectNodes(string.Format("/article[{0}]/hgroup/dl", articleIndex)) != null)
                    {
                        foreach (HtmlNode priceNode in childNode.SelectNodes(string.Format("/article[{0}]/hgroup/dl", articleIndex)))
                        {
                            if (priceNode.SelectNodes("dd").Count >= 1)
                                sbPriceInfo.AppendFormat("{0}:{1};",
                                    this.GetInnerText(priceNode.SelectSingleNode("dt")),
                                    this.GetInnerText(priceNode.SelectNodes("dd")[1]));
                        }
                    }

                    f.C_Remark = sbPriceInfo.ToString();
                    lstFlight.Add(f);
                    //string flightNo = this.GetInnerText(childNode.SelectSingleNode("/article[1]/hgroup[3]/ul[1]/li[1]/div[4]/div[@class='flightNo']"));
                    articleIndex++;
                }
            }

            return lstFlight;
        }