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 downloadStr = string.Empty; int cnt = 0; while (string.IsNullOrEmpty(downloadStr) && cnt < 2) { try { Thread thread = new Thread(delegate() { var p = new PageSnatch(); downloadStr = p.Navigate(strUrl, 50, "flight-info"); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); } catch (Exception ex) { throw ex; } cnt++; } HtmlDocument htmlPage = new HtmlDocument(); htmlPage.LoadHtml(downloadStr); HtmlNode docNode = htmlPage.DocumentNode; int articleIndex = 1; foreach (HtmlNode childNode in docNode.ChildNodes) { 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; }
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://flight.qunar.com/site/oneway_list.htm?searchDepartureAirport={0}&searchArrivalAirport={1}&searchDepartureTime={2}&searchArrivalTime=2015-11-03&nextNDays=0&startSearch=true&fromCode=CAN&toCode=BJS&from=qunarindex&lowestPrice=null", Server.UrlEncode(fromCity.C_NAME), Server.UrlEncode(toCity.C_NAME), dtDepart.ToString("yyyy-MM-dd")); string downloadStr = string.Empty; try { Thread thread = new Thread(delegate() { var p = new PageSnatch(); downloadStr = p.Navigate(strUrl, 50, "hdivResultPanel"); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); } catch (Exception ex) { throw ex; } HtmlDocument htmlPage = new HtmlDocument(); htmlPage.LoadHtml(downloadStr); HtmlNode docNode = htmlPage.DocumentNode; HtmlNodeCollection findChildNodes = docNode.ChildNodes; if (findChildNodes.Count == 1) { if (findChildNodes[0].Id.ToUpper().Equals("HDIVRESULTPANEL")) findChildNodes = docNode.ChildNodes[0].ChildNodes; } foreach (HtmlNode childNode in findChildNodes) { if (childNode.Id.ToUpper().StartsWith("ITEMBARXI")) { Flight f = new Flight(); f.C_DateSource = "QUNAR"; f.C_From = fromCity.C_NAME; f.C_To = toCity.C_NAME; f.C_Departure = departDate; StringBuilder sbOtherInfo = new StringBuilder(); if (this.GetHtmlNodeAttributeVal(childNode, "div/div", 1, 0).Equals("c1")) { HtmlNode checkNode = this.GetHtmlNode(childNode, "div/div", 1); f.C_Airline = this.GetInnerText(checkNode.SelectSingleNode("div[@class='vlc-wp']/div[@class='vlc-con']/div[@class='air-wp']/div[@class='air-row']/div[@class='a-name']")); f.C_FlightNo = this.GetInnerText(checkNode.SelectSingleNode("div[@class='vlc-wp']/div[@class='vlc-con']/div[@class='air-wp']/div[@class='air-row']/div[@class='a-model']/span")); } if (this.GetHtmlNodeAttributeVal(childNode, "div/div", 2, 0).Equals("c2")) { HtmlNode checkNode = this.GetHtmlNode(childNode, "div/div", 2); f.C_DEPTIME = this.GetInnerText(checkNode.SelectSingleNode("div[@class='a-dep-time']")); string depAirPort = this.GetInnerText(checkNode.SelectSingleNode("div[@class='a-dep-airport']")); sbOtherInfo.AppendFormat("departAirPort:{0};", depAirPort); } if (this.GetHtmlNodeAttributeVal(childNode, "div/div", 3, 0).Equals("c3")) { HtmlNode checkNode = this.GetHtmlNode(childNode, "div/div", 3); f.C_TotalTime = this.GetInnerText(checkNode.SelectSingleNode("div[@class='a-zh-wp']")); } if (this.GetHtmlNodeAttributeVal(childNode, "div/div", 4, 0).Equals("c4")) { HtmlNode checkNode = this.GetHtmlNode(childNode, "div/div", 4); f.C_ARRTIME = this.GetInnerText(checkNode.SelectSingleNode("div[@class='a-arr-time']")); string arrAirPort = this.GetInnerText(checkNode.SelectSingleNode("div[@class='a-arr-airport']")); sbOtherInfo.AppendFormat("arriveAirPort:{0};", arrAirPort); } if (this.GetHtmlNodeAttributeVal(childNode, ("div/div"), 7, 0).Equals("c7")) { HtmlNode findNode = this.GetHtmlNode(this.GetHtmlNode(childNode, "div/div", 7), "div/div", 0); if (findNode != null && findNode.ChildNodes.Count > 1) { HtmlNode mainPriceNode = findNode.ChildNodes[1]; HtmlNode node7 = this.GetHtmlNode(childNode, "div/div", 7); if (node7 != null && node7.SelectNodes("div/div").Count > 1) { if (node7.SelectNodes("div/div")[1].ChildNodes.Count > 1) mainPriceNode = node7.SelectNodes("div/div")[1].ChildNodes[1]; } if (mainPriceNode.Attributes["Style"] != null) { SortedList<string, string> lstPrice = new SortedList<string, string>(); if (mainPriceNode.SelectNodes("em").Count == 1) { HtmlNode calcutePriceNode = mainPriceNode.SelectSingleNode("em"); foreach (HtmlNode priceNode_b in calcutePriceNode.SelectNodes("b")) { if (priceNode_b.ChildNodes.Count > 1) { string styleValue = priceNode_b.Attributes["style"].Value; string style_LeftValue = styleValue.Split(new char[] { ';' })[1].ToLower().Replace("left: -", string.Empty).Replace("px", string.Empty).Trim(); int outLeftPX = 0; if (int.TryParse(style_LeftValue, out outLeftPX)) { foreach (HtmlNode detialPriceNode in priceNode_b.ChildNodes) { string key = string.Format("left:-{0}px", outLeftPX); lstPrice.Add(key, this.GetInnerText(detialPriceNode)); outLeftPX -= 16; } } else { lstPrice.Add("left:-16px", this.GetInnerText(priceNode_b.LastChild)); } } else { string styleValue = priceNode_b.Attributes["style"].Value.ToLower().Replace(" ", string.Empty); if (lstPrice.ContainsKey(styleValue)) lstPrice[styleValue] = this.GetInnerText(priceNode_b); else lstPrice.Add(styleValue, this.GetInnerText(priceNode_b)); } } } if (lstPrice.Count > 0) { decimal price = 0; int i = 1; foreach (var itmPrice in lstPrice) { price += Convert.ToInt16(itmPrice.Value) * i; i *= 10; } f.C_Price = price; } } } } f.C_Remark = sbOtherInfo.ToString(); lstFlight.Add(f); } } return lstFlight; }
/// <summary> /// this function can not get the pagesoure when the page with ajax, this just for remark. /// </summary> /// <param name="fromCity"></param> /// <param name="toCity"></param> /// <param name="departDate"></param> /// <returns></returns> private List<Flight> Download(string fromCity, string 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, toCity, dtDepart.ToString("yyMMdd")); var p = new PageSnatch(strUrl, 20000); p.Navigate(); if (p.Error != null) System.Diagnostics.Debug.Write(p.Error); else { System.Diagnostics.Debug.Write(p.HTMLSourceCode); System.Diagnostics.Debug.Write("=".PadLeft(50, '=')); System.Diagnostics.Debug.Write(p.TextAsync); } WebClient client = new WebClient(); string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.txt"); client.DownloadFile(new Uri(strUrl), path); byte[] pageData = client.DownloadData(new Uri(strUrl)); string pageHtml = Encoding.UTF8.GetString(pageData); System.Diagnostics.Debug.Write("Encoding.UTF8.GetString::" + pageHtml); using (StreamReader sr = new StreamReader(new MemoryStream(pageData))) { string strWebData = sr.ReadToEnd(); System.Diagnostics.Debug.Write(strWebData); } System.Diagnostics.Debug.WriteLine("=".PadLeft(20, '=')); System.Diagnostics.Debug.WriteLine("Method2:"); //Create a WebRequest to get the file HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(strUrl); //Create a response for this request HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse(); using (StreamReader reader = new StreamReader(fileResp.GetResponseStream(), Encoding.UTF8)) { string strWebData = reader.ReadToEnd(); System.Diagnostics.Debug.Write(strWebData); } HttpWebRequest fileReq1 = (HttpWebRequest)HttpWebRequest.Create(strUrl); HttpWebResponse fileResp1 = (HttpWebResponse)fileReq1.GetResponse(); string path1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test1.txt"); //Get the Stream returned from the response using (Stream responseStream = fileResp1.GetResponseStream()) { using (FileStream localFileStream = new FileStream(path1, FileMode.Create)) { var buffer = new byte[4096]; long totalBytesRead = 0; int bytesRead; while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0) { totalBytesRead += bytesRead; localFileStream.Write(buffer, 0, bytesRead); } } } System.Diagnostics.Debug.WriteLine("=".PadLeft(20, '=')); System.Diagnostics.Debug.WriteLine("Method3:"); HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(strUrl); wReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)"; wReq.Method = "GET"; wReq.Timeout = 12000; HttpWebResponse wResp = (HttpWebResponse)wReq.GetResponse(); Stream respStream = wResp.GetResponseStream(); using (StreamReader reader = new StreamReader(respStream, Encoding.UTF8)) { string strWebData = reader.ReadToEnd(); System.Diagnostics.Debug.Write(strWebData); } return lstFlight; }