詳解C#如何利用爬蟲技術(shù)實(shí)現(xiàn)快捷租房
場景
做為一個碼農(nóng),大部分都集中在一二線城市,所以租房也就無可避免,面對如今五花八門的租房信息,往往很難找到合適的房子。而如今的這些租房軟件,大部分也都被中介、廣告等給占據(jù)了。除了去中介公司,感覺再也找不到合適的房子,但是面對50%的中介費(fèi),很大程度上也難以忍受。偶然之間聽朋友說到可以去豆瓣看看,然后我就試著去尋找一下,結(jié)果發(fā)現(xiàn)豆瓣討論組的信息太過雜亂,先不說房源的好壞,光是找合適位置的帖子就得翻好久。。。
需求
所以,綜上場景所述,就寫了個簡單的爬蟲來爬取需要的位置以及最新發(fā)布的一些房源帖子。這里實(shí)現(xiàn)比較簡單,同樣豆瓣也有做一些防爬處理(IP訪問次數(shù)等),而我并不是為了獲取更多的信息而爬取,只是為了方便。所以既沒有多線程處理,也沒有做反防爬。每次獲取的信息也足夠看了,不行的話就等第二天再看唄。
開發(fā)環(huán)境
.NET Framework版本:4.5
開發(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("請輸入城市!");
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);
}
}實(shí)現(xiàn)效果

代碼解析:主要是使用了HttpGet來獲取鏈接的網(wǎng)頁信息,然后使用正則匹配我們的需求數(shù)據(jù)。每次只訪問100個討論組,每個討論組內(nèi)獲取第一頁的帖子信息。如果不使用代理ip或者其他反防爬機(jī)制的話,基本上這個軟件每天只使用一次就達(dá)到上限了(獲取到的數(shù)據(jù)為空等);要是沒有合適的帖子的話,可以第二天再查找一下。
到此這篇關(guān)于詳解C#如何利用爬蟲技術(shù)實(shí)現(xiàn)快捷租房的文章就介紹到這了,更多相關(guān)C#租房內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#調(diào)用百度地圖API根據(jù)地名獲取經(jīng)緯度geocoding
本文主要介紹了C#調(diào)用百度地圖API根據(jù)地名獲取經(jīng)緯度geocoding,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
C#采用OpenXml實(shí)現(xiàn)給word文檔添加文字
這篇文章主要介紹了C#采用OpenXml實(shí)現(xiàn)給word文檔添加文字的方法,包括了用法的實(shí)例分析,是非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09
無法從 int? 轉(zhuǎn)換為 int 運(yùn)行時出現(xiàn)錯誤
無法從"int?"轉(zhuǎn)換為"int" ,在運(yùn)行時會出現(xiàn)錯誤,通過強(qiáng)制類型轉(zhuǎn)換(int)便可解決2014-05-05

