詳解C#如何利用爬蟲(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)文章
C#制作鷹眼的詳細(xì)全過(guò)程(帶注釋)實(shí)例代碼
C#制作鷹眼的詳細(xì)全過(guò)程(帶注釋)實(shí)例代碼,需要的朋友可以參考一下2013-03-03DataGridView不顯示最下面的新行、判斷新增行、刪除行操作
這篇文章介紹了DataGridView不顯示最下面的新行、判斷新增行、刪除行的操作方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02使用數(shù)字簽名實(shí)現(xiàn)數(shù)據(jù)庫(kù)記錄防篡改(Java實(shí)現(xiàn))
本文主要介紹了Java中使用數(shù)字簽名實(shí)現(xiàn)數(shù)據(jù)庫(kù)記錄防篡改的方法與步驟。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01C# NullReferenceException解決案例講解
這篇文章主要介紹了C# NullReferenceException解決案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08UGUI ScrollRect實(shí)現(xiàn)帶按鈕翻頁(yè)支持拖拽
這篇文章主要為大家詳細(xì)介紹了UGUI ScrollRect實(shí)現(xiàn)帶按鈕翻頁(yè)支持拖拽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05協(xié)定需要會(huì)話,但是綁定“BasicHttpBinding”不支持它或者因配置不正確而無(wú)法支持它
在IIS7及以上版本服務(wù)器中提供了基于WAS的無(wú).SVC文件的WCF服務(wù)激活功能,能夠提供基于HTTP和非HTTP協(xié)議的訪問(wèn),通過(guò)添加Windows Server AppFabric可以更方便的管理WCF服務(wù)2012-12-12