/// <summary> /// 填充错误数据 /// </summary> public static HotelInfo SetHotel(string HotelId, string HotelName, string RoomName, int _id, string date) { HotelInfo hotel = new HotelInfo(); hotel.HotelId = HotelId; hotel.HotelName = HotelName; hotel.Bed = RoomName; hotel.Id = _id; hotel.Price = "0"; hotel.Breakfast = ""; hotel.Date = date; hotel.Count = "0"; hotel.CrawlerTime = DateTime.Now.ToString("yyyy-MM-dd-HHmmss"); return(hotel); }
/// <summary> /// 获取酒店价格 /// </summary> /// <param name="url"></param> /// <param name="ent"></param> public static void GetPriceList(string url, Unit ent, object BeginNum, int flowId) { hotelnum++; //临时ID string _UID = ""; //清空 eid = ""; //线程状态 bool Issthread = true; try { DateTime hote_date = ent.ComeDate; //参数 ent.antitoken = "722b1952bc9ceedbc0b04eb863cdc9ae"; ent.iid = "0.16545553576668293"; ent.TraceId = "007df812-1536-4ede-a0cd-3076dc576c0e"; ent.trace_token = "|*|cityId:101|*|qId:08d9dd45-e884-42cd-b572-43515aa5c96c|*|st:city|*|sId:101|*|pos:4|*|hId:40601023|*|Tp:talRec|*|"; ent.search_entrance_id = "tpc_home"; List <HotelInfo> HotelInfoList2 = new List <HotelInfo>(); //读取需求列表 StreamReader sr = new StreamReader(@shujupath, Encoding.GetEncoding("utf-8")); string[] upStr = File.ReadAllLines(@shujupath, Encoding.GetEncoding("utf-8")); sr.Close(); int _id = 0; int HotelCount = upStr.Length; Console.WriteLine("共........." + HotelCount + "家酒店"); if (Issthread) { try { //请求是否出错 bool iserr = false; Console.WriteLine("第" + flowId + "条线程正在爬取第........." + hotelnum + "家酒店,还有" + (300 - hotelnum) + "家"); string[] newStr = upStr[(int)BeginNum].Split('\t'); HotelId = newStr[1]; _UID = newStr[1]; //填充房间URL var new_url = child_url.Replace("{HotelId}", newStr[1]); new_url = new_url.Replace("{ComeDate}", ent.ComeDate.ToString("yyyy-MM-dd")); new_url = new_url.Replace("{LeaveDate}", ent.LeaveDate.ToString("yyyy-MM-dd")); new_url = new_url.Replace("{antitoken}", ent.antitoken); new_url = new_url.Replace("{trace_token}", ent.trace_token.ToString()); new_url = new_url.Replace("{i}", 1.ToString()); var rerferer = rfer.Replace("{HotelId}", newStr[1]); //获取房间信息 RoomRequest RoomRequestlist = JsonConvert.DeserializeObject(HttpGet(new_url, rerferer, newcookieStr), typeof(RoomRequest)) as RoomRequest; if (RoomRequestlist == null) { iserr = true; } else if (RoomRequestlist.Status == "400" || RoomRequestlist.RoomList == null) { iserr = true; } if (iserr) { Console.WriteLine("获取" + newStr[0] + "房间信息失败XXXXXXXX"); HotelInfo hotel = SetHotel(newStr[1], newStr[0], "", _id, hote_date.ToString("yyyy-MM-dd")); HotelInfoList2.Add(hotel); return; } Console.WriteLine("成功获取" + newStr[0] + "房间信息√√√√√√√√√√"); //循环房型 foreach (var room_ent in RoomRequestlist.RoomList) { //循环付款类型 foreach (var Pulic_ent in room_ent.PolicyInfo) { //BookType:2 在线付 if (Pulic_ent.BookType == "2") { for (var d = 0; d < 7; d++) { //填充cookie的pageurl string _pageurl = page_url.Replace("{HotelId}", newStr[1]); _pageurl = _pageurl.Replace("{RoomTypeId}", room_ent.RoomId); _pageurl = _pageurl.Replace("{ComeDate}", ent.ComeDate.AddDays(d).ToString("yyyy-MM-dd")); _pageurl = _pageurl.Replace("{LeaveDate}", ent.LeaveDate.AddDays(d).ToString("yyyy-MM-dd")); _pageurl = _pageurl.Replace("{PolicyId}", Pulic_ent.PolicyId); _pageurl = _pageurl.Replace("{SRId}", Pulic_ent.SRId); //设置新的cookie TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0); string new_coo = rnewcookieStr.Replace("{pageurl}", _pageurl); new_coo = new_coo.Replace("{firsttime}", Convert.ToInt64(ts.TotalSeconds).ToString()); new_coo = new_coo.Replace("{lasttime}", Convert.ToInt64(ts.TotalSeconds + 10000).ToString()); new_coo = new_coo.Replace("{longkey}", Convert.ToInt64(ts.TotalSeconds + 10001).ToString()); //填充订单URL var new_room_url = room_url.Replace("{HotelId}", newStr[1]); new_room_url = new_room_url.Replace("{RoomTypeId}", room_ent.RoomId); new_room_url = new_room_url.Replace("{PolicyId}", Pulic_ent.PolicyId); new_room_url = new_room_url.Replace("{ShadowId}", Pulic_ent.SRId); new_room_url = new_room_url.Replace("{ComeDate}", ent.ComeDate.AddDays(d).ToString("yyyy-MM-dd")); new_room_url = new_room_url.Replace("{LeaveDate}", ent.LeaveDate.AddDays(d).ToString("yyyy-MM-dd")); new_room_url = new_room_url.Replace("{antitoken}", ent.antitoken); //填充订单referer var new_referer = newrfer.Replace("{HotelId}", newStr[1]); new_referer = new_referer.Replace("{RoomTypeId}", room_ent.RoomId); new_referer = new_referer.Replace("{ComeDate}", ent.ComeDate.AddDays(d).ToString("yyyy-MM-dd")); new_referer = new_referer.Replace("{LeaveDate}", ent.LeaveDate.AddDays(d).ToString("yyyy-MM-dd")); new_referer = new_referer.Replace("{PolicyId}", Pulic_ent.PolicyId); new_referer = new_referer.Replace("{SRId}", Pulic_ent.SRId); bool isrspent = false; string txt = ""; HotelInfo hotel = new HotelInfo(); //4分钟获取一次代理 if ((DateTime.Now.TimeOfDay.TotalMinutes - proxytime) > 4) { //GetProxy(); } //获取订单信息 rsp rspent = JsonConvert.DeserializeObject(HttpNewGet(new_room_url, new_referer, new_coo, 0).Result, typeof(rsp)) as rsp; //如果没有返回值 if (rspent == null) { isrspent = true; txt = "请求失败"; } else if (string.IsNullOrEmpty(rspent.BookMaxRoomCount)) { isrspent = true; txt = "没有库存"; } else { Console.WriteLine("成功获取" + newStr[0] + "," + room_ent.RoomName + "订单信息√√√√√√√√√√"); var PriceList = rspent.bookDayPriceList; hotel.HotelId = newStr[1]; hotel.HotelName = newStr[0]; hotel.Bed = room_ent.RoomName; hotel.Id = _id; hotel.Price = PriceList[0].RealTimePrice == "满房" ? "0" : PriceList[0].RealTimePrice; hotel.Breakfast = PriceList[0].Breakfast == "" ? "无早" : PriceList[0].Breakfast; hotel.Date = PriceList[0].Time.ToString("yyyy-MM-dd"); hotel.Count = rspent.BookMaxRoomCount == null ? "0" : rspent.BookMaxRoomCount; } hotel.CrawlerTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); if (isrspent) { Console.WriteLine("获取" + newStr[0] + "," + room_ent.RoomName + "订单信息失败XXXXXXX" + txt); hotel = SetHotel(newStr[1], newStr[0], room_ent.RoomName, _id, hote_date.AddDays(d).ToString("yyyy-MM-dd")); } HotelInfoList2.Add(hotel); } break; } } } } catch (Exception ex) { eid += _UID; string log = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "酒店ID:" + _UID + ex.ToString(); Console.WriteLine(log); return; } Thread.Sleep(1000); //下载 Console.WriteLine("第" + flowId + "条线程已爬取共........." + HotelInfoList2.Count + "条数据"); DownList(HotelInfoList2); } } catch (Exception ex) { string log = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "酒店ID:" + _UID + ex.ToString(); Console.WriteLine(log); WriteTxt(log, logpath + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ".txt"); } }