欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解C#如何利用爬蟲(chóng)技術(shù)實(shí)現(xiàn)快捷租房

 更新時(shí)間:2022年09月16日 09:24:12   作者:Csharp小記  
做為一個(gè)碼農(nóng),大部分都集中在一二線城市,所以租房也就無(wú)可避免,面對(duì)如今五花八門(mén)的租房信息,往往很難找到合適的房子。本文教你如何利用爬蟲(chóng)技術(shù)實(shí)現(xiàn)快捷租房,感興趣的可以了解一下

場(chǎng)景

做為一個(gè)碼農(nóng),大部分都集中在一二線城市,所以租房也就無(wú)可避免,面對(duì)如今五花八門(mén)的租房信息,往往很難找到合適的房子。而如今的這些租房軟件,大部分也都被中介、廣告等給占據(jù)了。除了去中介公司,感覺(jué)再也找不到合適的房子,但是面對(duì)50%的中介費(fèi),很大程度上也難以忍受。偶然之間聽(tīng)朋友說(shuō)到可以去豆瓣看看,然后我就試著去尋找一下,結(jié)果發(fā)現(xiàn)豆瓣討論組的信息太過(guò)雜亂,先不說(shuō)房源的好壞,光是找合適位置的帖子就得翻好久。。。

需求

所以,綜上場(chǎng)景所述,就寫(xiě)了個(gè)簡(jiǎn)單的爬蟲(chóng)來(lái)爬取需要的位置以及最新發(fā)布的一些房源帖子。這里實(shí)現(xiàn)比較簡(jiǎn)單,同樣豆瓣也有做一些防爬處理(IP訪問(wèn)次數(shù)等),而我并不是為了獲取更多的信息而爬取,只是為了方便。所以既沒(méi)有多線程處理,也沒(méi)有做反防爬。每次獲取的信息也足夠看了,不行的話就等第二天再看唄。

開(kāi)發(fā)環(huán)境

.NET Framework版本:4.5

開(kāi)發(fā)工具

Visual Studio 2013

實(shí)現(xiàn)代碼

private readonly string douBanUrl = "https://www.douban.com/group/search?cat=1019&sort=time";

        bool isStop = false;

        public FormCrawler()
        {
            InitializeComponent();
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txt_city.Text))
            {
                MessageBox.Show("請(qǐng)輸入城市!");
                return;
            }
            if (string.IsNullOrWhiteSpace(txt_keys.Text))
            {
                MessageBox.Show("請(qǐng)輸入關(guān)鍵字!");
                return;
            }

            //初始化控件
            btn_start.Enabled = false;
            btn_stop.Enabled = true;
            listView1.Items.Clear();
            progressBar1.Value = 0;

            List<string> groups = GetGroupUrls();
            progressBar1.Maximum = groups.Count;
            Task.Run(() =>
            {
                GetHouseInfo(groups);

                this.BeginInvoke(new Action(() =>
                {
                    btn_start.Enabled = true;
                    btn_stop.Enabled = false;
                }));
            });


        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            isStop = true;
            btn_start.Enabled = true;
            btn_stop.Enabled = false;
            progressBar1.Value = progressBar1.Maximum;
        }

     
        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListView.SelectedListViewItemCollection selectItem = listView1.SelectedItems;
            if (selectItem.Count > 0)
            {
                Process.Start(selectItem[0].SubItems[1].Text);
            }
        }
/// <summary>
        /// 獲取前100個(gè)討論組
        /// </summary>
        /// <returns></returns>
        private List<string> GetGroupUrls()
        {
            List<string> groupUrls = new List<string>();

            int pageSize = 20;//豆瓣每頁(yè)顯示20個(gè)討論組
            string groupUrl = string.Empty;

            for (int groupNum = 0; groupNum <= 80; groupNum += pageSize)
            {
                if (isStop) break;

                groupUrl = string.Format(douBanUrl + "&q={0}&start={1}", HttpUtility.UrlEncode(txt_city.Text + "租房", Encoding.UTF8), groupNum);

                string text = HttpUtil.HttpGet(groupUrl);

                if (!string.IsNullOrWhiteSpace(text))
                {
                    Regex reg = new Regex(@"(?is)(?<=<div\sclass=""pic""[^>]*?>).*?(?=</div>)");
                    MatchCollection matchs = reg.Matches(text);
                    for (int i = 0; i < matchs.Count; i++)
                    {
                        if (matchs[i].Success)
                        {
                            Regex regHref = new Regex("(?<=href\\s*=\\s*\")\\S+(?<!\")");
                            string href = regHref.Match(matchs[i].Value).Value;
                            groupUrls.Add(href);
                        }
                    }
                }
            }

            return groupUrls;
        }

        /// <summary>
        /// 獲取討論組內(nèi)符合條件的房源
        /// </summary>
        /// <param name="urls"></param>
        private void GetHouseInfo(List<string> urls)
        {
            for (int u = 0; u < urls.Count; u++)
            {
                if (isStop) break;
                this.BeginInvoke(new Action(() =>
                {
                    progressBar1.Value++;
                }));

                string text = HttpUtil.HttpGet(urls[u] + "/discussion?start=");
                Regex regex = new Regex(@"(?is)(?<=<tr\sclass=""""[^>]*?>).*?(?=</tr>)");
                MatchCollection matchs = regex.Matches(text);
                for (int i = 0; i < matchs.Count; i++)
                {
                    if (matchs[i].Success)
                    {
                        #region 匹配時(shí)間
                        Regex regDate = new Regex(@"(?is)(?<=<td\snowrap=""nowrap""\sclass=""time""[^>]*?>).*?(?=</td>)");
                        Match matchDate = regDate.Match(matchs[i].Value);
                        if (!matchDate.Success)
                        {
                            continue;
                        }
                        string time = matchDate.Value;
                        DateTime dtTemp = new DateTime();
                        if (!DateTime.TryParse(time, out dtTemp))
                        {
                            time = time.Insert(0, DateTime.Now.Year + "-");
                            dtTemp = Convert.ToDateTime(time);
                        }
                        if (dtTemp < dateTimePicker1.Value.Date)
                        {
                            continue;
                        }
                        #endregion

                        #region 獲取標(biāo)題

                        Regex regTitle = new Regex("(?<=title\\s*=\\s*\")\\s*\\S*(?<!\")");
                        Match matchTitle = regTitle.Match(matchs[i].Value);
                        if (matchTitle.Success)
                        {
                            string title = matchTitle.Value;
                            #region 匹配標(biāo)題
                            bool isContain = false;
                            string[] keys = txt_keys.Text.Split('*');
                            foreach (string key in keys)
                            {
                                if (matchTitle.Value.Contains(key))
                                {
                                    isContain = true;
                                }
                            }
                            #endregion
                            if (isContain)
                            {
                                //獲取鏈接
                                Regex regHref = new Regex("(?<=href\\s*=\\s*\")\\s*\\S*(?<!\")");
                                string href = regHref.Match(matchs[i].Value).Value;

                                AddUI(title, href, time);
                            }
                        }
                        #endregion
                    }

                }
            }
        }


        private void AddUI(string title, string href, string time)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action(() => AddUI(title, href, time)));
            }
            else
            {
                ListViewItem lvitem = new ListViewItem(new string[3] { title, href, time });
                listView1.Items.Add(lvitem);
            }
        }

實(shí)現(xiàn)效果

代碼解析:主要是使用了HttpGet來(lái)獲取鏈接的網(wǎng)頁(yè)信息,然后使用正則匹配我們的需求數(shù)據(jù)。每次只訪問(wèn)100個(gè)討論組,每個(gè)討論組內(nèi)獲取第一頁(yè)的帖子信息。如果不使用代理ip或者其他反防爬機(jī)制的話,基本上這個(gè)軟件每天只使用一次就達(dá)到上限了(獲取到的數(shù)據(jù)為空等);要是沒(méi)有合適的帖子的話,可以第二天再查找一下。

到此這篇關(guān)于詳解C#如何利用爬蟲(chóng)技術(shù)實(shí)現(xiàn)快捷租房的文章就介紹到這了,更多相關(guān)C#租房?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論