【发布时间】:2012-01-15 19:37:14
【问题描述】:
我正在开发一个简单的网络爬虫来获取 URL,爬取网站上的一级链接并使用 RegEx 从所有页面中提取邮件...
我知道这有点草率,这只是开始,但我总是在运行脚本 2 分钟后得到“操作超时”..
private void button1_Click(object sender, System.EventArgs e)
{
string url = textBox1.Text;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string code = sr.ReadToEnd();
string re = "href=\"(.*?)\"";
MatchCollection href = Regex.Matches(code, @re, RegexOptions.Singleline);
foreach (Match h in href)
{
string link = h.Groups[1].Value;
if (!link.Contains("http://"))
{
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(url + link);
HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
StreamReader sr2 = new StreamReader(response.GetResponseStream());
string innerlink = sr.ReadToEnd();
MatchCollection m2 = Regex.Matches(code, @"([\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?)", RegexOptions.Singleline);
foreach (Match m in m2)
{
string email = m.Groups[1].Value;
if (!listBox1.Items.Contains(email))
{
listBox1.Items.Add(email);
}
}
}
}
sr.Close();
}
【问题讨论】:
-
帮您解决什么问题?提出具体问题以获得答案。
-
为什么要重新发明轮子,Html Agility Pack 让生活变得更轻松。 htmlagilitypack.codeplex.com
-
我需要帮助找出导致超时的原因..
标签: c# web-crawler