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