static void Main(string[] args) { Process[] processes = Process.GetProcessesByName("Scraping-Rightmove"); if (processes.Count() > 1) { MessageBox.Show("Scraping-Rightmove.exe is already running now. So it will be closed."); return; } KillProcessAndChildren(_browserName); var filepath = Path.GetDirectoryName(Application.ExecutablePath) + _defaultFileName; if (File.Exists(filepath)) { File.Delete(filepath); } _streamWriter = new StreamWriter(filepath, true, Encoding.Unicode); RoomInfo roominfo = new RoomInfo(); roominfo.Address = "ADDRESS"; roominfo.Price = "PRICE"; roominfo.AddOn = "ADD ON"; roominfo.LetAvailableDate = "LET AVAILABLE DATE"; roominfo.LetType = "LET TYPE"; roominfo.FurnishType = "FURNISH TYPE"; roominfo.PropertyType = "PROPERTY TYPE"; roominfo.BedRoom = "BED ROOM"; roominfo.BathRoom = "BATH ROOM"; roominfo.Size = "SIZE"; roominfo.MarkedBy = "MARKED BY"; _streamWriter.WriteLine(roominfo.GetRoomInfo()); _streamWriter.Flush(); ProcScraping(); }
private static string InvestResource(IWebDriver webDrv, StreamWriter streamWriter, Dictionary <int, string> paths) { try { foreach (var path in paths) { webDrv.Navigate().GoToUrl(path.Value); Thread.Sleep(2000); var main = webDrv.FindElement(By.TagName(@"main")); var divElements = main.FindElements(By.TagName(@"div")); RoomInfo roominfo = new RoomInfo(); roominfo.Address = divElements[2].Text; roominfo.Price = divElements[9].Text.Replace("\r\n", string.Empty); roominfo.AddOn = divElements[14].Text; var strElements = Regex.Replace(main.Text, "<.*?>", string.Empty); var elements = strElements.Split(new string[] { "\r\n" }, StringSplitOptions.None); for (int i = 0; i < 18; i++) { var strElement = elements[i]; if (strElement.Contains(@"Let available date: ")) { roominfo.LetAvailableDate = strElement.Replace(@"Let available date: ", string.Empty); continue; } if (strElement.Contains(@"Let type: ")) { roominfo.LetType = strElement.Replace(@"Let type: ", string.Empty); continue; } if (strElement.Contains(@"Furnish type: ")) { roominfo.FurnishType = strElement.Replace(@"Furnish type: ", string.Empty); continue; } if (strElement == "PROPERTY TYPE") { roominfo.PropertyType = elements[++i]; continue; } if (strElement == "BEDROOMS") { roominfo.BedRoom = elements[++i].Substring(1); continue; } if (strElement == "BATHROOMS") { roominfo.BathRoom = elements[++i].Substring(1); continue; } if (strElement == "SIZE") { roominfo.Size = elements[++i]; continue; } } var aside = webDrv.FindElement(By.TagName(@"aside")); roominfo.MarkedBy = aside.FindElements(By.TagName(@"a"))[0].Text; streamWriter.WriteLine(roominfo.GetRoomInfo()); streamWriter.Flush(); } return(""); } catch (Exception err) { return(null); } }