Exemplo n.º 1
0
        public List<Property> Properties(string query)
        {
            var results = new List<Property>();
            try
            {
                var driver = new PhantomJSDriver();

                driver.Navigate().GoToUrl(query);
                var body = driver.FindElementByTagName("body");
                Console.Write(body.Text);
                Thread.Sleep(2000);
                var articles = driver.FindElementsByTagName("article");

                foreach (var webElement in articles)
                {
                    try
                    {
                        var newProperty = new Property
                        {
                            Thumbnail = webElement.FindElement(By.ClassName("photo")).GetAttribute("data-photourl"),
                            Link = webElement.FindElement(By.ClassName("hdp-link")).GetAttribute("href")
                        };
                        var fullAddress = webElement.FindElement(By.ClassName("image-loaded")).GetAttribute("alt");
                        var split = fullAddress.Split(',');
                        var splitCount = split.Count();
                        var city = split[splitCount - 2];
                        var state = split.Last();
                        newProperty.Address = split[0];
                        newProperty.City = city;
                        newProperty.State = state;
                        results.Add(newProperty);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error searching Zillow. " + ex.ToString());
                    }
                }

                driver.Quit();

                return results.ToList();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error searching Zillow. " + ex.ToString());
            }
            return null;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 使用 JS 引擎请求页面
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cookieContainer"></param>
        /// <returns></returns>
        public string QueryRequest(string url, CookieContainer cookieContainer = null)
        {
            var domain = Regex.Matches(url, "(?i)(https*?://)*(.+?)/")[0].Result("$2");

            var index = domain.IndexOf(".", StringComparison.Ordinal);

            if (index != domain.LastIndexOf(".", StringComparison.Ordinal))
            {
                domain = domain.Substring(index);
            }

            cookieContainer.GetAllCookies().ForEach(f =>
            {
                _driver.Manage().Cookies.AddCookie(new OS.Cookie(f.Name, f.Value, domain, "/", DateTime.Now.AddDays(1)));
            });

            _driver.Url = url;

            return(_driver.FindElementByTagName("body").Text);
        }
Exemplo n.º 3
0
        public Property Property(Property property)
        {
            try
            {
                var query = string.Format("http://www.zillow.com/homedetails/{0}_zpid/", property.Zpid);
                var driver = new PhantomJSDriver();

                driver.Navigate().GoToUrl(query);
                var body = driver.FindElementByTagName("body");
                Console.Write(body.Text);
                Thread.Sleep(2000);
                var webElement = driver.FindElement(By.ClassName("main-wrapper"));
                property.Link = query;

                    try
                    {
                        var thumbnailElem = webElement.FindElement(By.ClassName("hip-photo"));
                        if (thumbnailElem != null)
                        {
                            property.Thumbnail = thumbnailElem.GetAttribute("src");
                        }

                        var fullAddressElem = webElement.FindElement(By.TagName("h1"));
                        if (fullAddressElem != null)
                        {
                            var split = fullAddressElem.Text.Split(',');
                            property.Address = split[0];
                        }

                        var cityElem = webElement.FindElement(By.ClassName("addr_city"));
                        if (cityElem != null)
                        {
                            var split1 = cityElem.Text.Split(',');
                            var split2 = split1[1].Split(' ');
                            property.City = split1[0];
                            property.State = split2[1];
                        }

                        var priceElem = webElement.FindElement(By.ClassName("main-row"));
                        if (priceElem != null)
                        {
                            if (priceElem.Text.Contains("$"))
                            {
                                property.Price = Convert.ToDouble(priceElem.Text.Replace("$", "").Replace("from: ", ""));
                            }
                        }

                        var valueChangeElem = webElement.FindElement(By.ClassName("zest-change"));
                        if (valueChangeElem != null)
                        {
                            var split = valueChangeElem.Text.Split(' ');
                            property.ValueChange = Convert.ToDouble(split[0].Replace("$", "").Replace(",", ""));
                        }

                        var zestimateElms = webElement.FindElements(By.ClassName("zest-value"));
                        foreach (var zestimateElm in zestimateElms)
                        {
                            var text = zestimateElm.Text;
                            if (text.Contains("/mo"))
                            {
                                property.RentZestimate = Convert.ToDouble(text.Replace("/mo", "").Replace("$", ""));
                            }
                            else if(!text.Contains("X"))
                            {
                                property.Zestimate = Convert.ToDouble(text.Replace("$", ""));
                            }
                        }

                        var zsgListUl = webElement.FindElements(By.ClassName("zsg-list_square"));
                        foreach (var element in zsgListUl)
                        {
                            var lis = element.FindElements(By.TagName("li"));
                            foreach (var li in lis)
                            {
                                var text = li.Text;
                                if (text.Contains("Family"))
                                {
                                    property.PropertyType = text;
                                }
                                if (text.Contains("Built in"))
                                {
                                    var modifiedBuiltIn = text.Replace("Built in ", "");
                                    property.YearBuilt = Convert.ToInt32(modifiedBuiltIn);
                                }
                                if (text.Contains("Lot"))
                                {
                                    var modifiedSqft = text.Replace("Lot: ", "").Replace(" sqft", "").Replace(",","");
                                    if (modifiedSqft.Contains("acres"))
                                    {
                                        modifiedSqft = modifiedSqft.Replace(" acres", "");
                                        property.LotSizeSqFt = (int) (Convert.ToDouble(modifiedSqft)*43560);
                                    }
                                    else
                                    {
                                        property.LotSizeSqFt = Convert.ToInt32(modifiedSqft);
                                    }
                                }
                                if (text.Contains("Unit count: "))
                                {
                                    property.NumUnits = Convert.ToInt32(text.Replace("Unit count: ", ""));
                                }
                                if (text.Contains("on Zillow"))
                                {
                                    var textSplit = text.Split(' ');
                                    property.DaysOnZillow = Convert.ToInt32(textSplit[0]);
                                }
                            }
                        }

                        property.AnnualTaxes = property.Price * .01;

                        var addrBbsElms = webElement.FindElements(By.ClassName("addr_bbs"));
                        foreach (var addrBbs in addrBbsElms)
                        {
                            var text = addrBbs.Text;
                            if (text.Contains("sqft"))
                            {
                                var splitSqft = text.Split(' ');
                                property.FinishedSqFt = Convert.ToInt32(splitSqft[0].Replace(",",""));
                            }
                            if (text.Contains("bath"))
                            {
                                var formatedBaths = text.Replace("bath", "").Replace("--", "").Replace(" ", "").Replace("s","");
                                if (formatedBaths != "")
                                {
                                    property.Bathrooms = Convert.ToInt32(formatedBaths);
                                }
                            }
                            if (text.Contains("bed"))
                            {
                                var formattedBds = text.Replace("bed", "").Replace("--", "").Replace(" ", "").Replace("s", "");
                                if (formattedBds != "")
                                {
                                    property.Bedrooms = Convert.ToInt32(formattedBds);
                                }
                            }
                        }

                        var statusElem = webElement.FindElement(By.ClassName("status-icon-row"));
                        if (statusElem != null)
                        {
                            property.Status = statusElem.Text;
                        }

                        var descElem = webElement.FindElement(By.ClassName("notranslate"));
                        if (descElem != null)
                        {
                            property.HomeDescription = descElem.Text;
                        }

                        var insuranceElem = webElement.FindElement(By.Id("home-ins-out"));
                        if (insuranceElem != null)
                        {
                            property.Insurance = Convert.ToDouble(insuranceElem.Text.Replace("$", ""));
                        }

                        property.LoanAmount = property.Price * .80;
                        property.LoanType = "30 year fixed";
                        property.LoanTerm = 360;

                        //var interestElem = webElement.FindElements(By.XPath("//*[@id='zmm-calc-input-program']/option"));
                        //if (interestElem != null)
                        //{
                        //    newProperty.InterestRate = Convert.ToDouble(interestElem[0].Text);
                        //}

                        property.DownPayment = property.Price * .20;

                        var monthlyPaymentElem = webElement.FindElement(By.Id("pi-payment-out"));
                        if (monthlyPaymentElem != null)
                        {
                            property.DebtServicePI = Convert.ToDouble(monthlyPaymentElem.Text.Replace("$", ""));
                        }

                        property.ClosingCosts = property.Price * .025;
                        if (property.CurrentRent < 1)
                        {
                            property.CurrentRent = property.RentZestimate;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error searching Zillow. " + ex.ToString());
                    }

                driver.Quit();
                return property;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error searching Zillow. " + ex.ToString());
            }
            return null;
        }