亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

場景

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

需求

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

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

.NET Framework版本:4.5

開發(fā)工具

Visual Studio 2013

實現(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("請輸入城市!");
                return;
            }
            if (string.IsNullOrWhiteSpace(txt_keys.Text))
            {
                MessageBox.Show("請輸入關(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個討論組
        /// </summary>
        /// <returns></returns>
        private List<string> GetGroupUrls()
        {
            List<string> groupUrls = new List<string>();

            int pageSize = 20;//豆瓣每頁顯示20個討論組
            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 匹配時間
                        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);
            }
        }

實現(xiàn)效果

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

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

相關(guān)文章

最新評論