倾家荡产--求解获取网页内容; System.Net.WebClient获取数据,再用正则分析数据,获取指定内容 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ///获取网页源文件private string GetHtmlSource(string url) { string html = ""; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("GB2312")); html = reader.ReadToEnd(); } catch { MessageBox.Show("无法连接到远程服务器,请检查您的网络是否正常!", "提示"); } return html; } #endregion http://www.cnblogs.com/skyiv/archive/2005/10/01/GetIP.html这里有“获取网页的HTML内容”的代码。 我想多给几个链接,让大家参考一下:http://www.whitepages.com/5116/search/FindPerson?firstname=&name=wa&name_begins_with=1&city_zip=+01002&state_id=All+US&x=53&y=16 try//string src = GetHtmlSource("http://www.whitepages.com/5116/search/Replay?search_id=40051390946113532971");string src = GetHtmlSource("http://www.whitepages.com/5116/search/FindPerson?firstname=&name=wa&name_begins_with=1&city_zip=+01002&state_id=All+US&x=53&y=16");MatchCollection mc = Regex.Matches(src, @"<div\s+class=""description"">\s*<h2><a[^>]*>(?<name>[\s\S]*?)</a>\s*</h2>\s*<p>(?<adress>[\s\S]*?)</p>\s*<p>(?<code>[\s\S]*?)</p>\s*<p[^>]*>(?<phone>[\s\S]*?)</p>", RegexOptions.IgnoreCase);foreach (Match m in mc){ richTextBox2.Text += m.Groups["name"].Value + "\n"; richTextBox2.Text += m.Groups["adress"].Value + "\n"; richTextBox2.Text += m.Groups["code"].Value + "\n"; richTextBox2.Text += m.Groups["phone"].Value + "\n\n";}private string GetHtmlSource(string url){ System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24"; System.Net.WebResponse response = request.GetResponse(); System.IO.Stream resStream = response.GetResponseStream(); System.IO.StreamReader sr = new System.IO.StreamReader(resStream, System.Text.Encoding.UTF8); string htmlSource = (sr.ReadToEnd()); resStream.Close(); sr.Close(); return htmlSource; } 谢谢,lxcnn(过客) ;问题已解决...... 谢谢,lxcnn(过客) ;问题已解决...... 收藏 0分享 0 Code First Power Tool牛X工具不能用了?求解释!!! 编码将wav文件转为MP3文件 请教一个多线程中的委托的问题 简单正则表达式 如何使用正则匹配重复的字符串? 要放假啦 高手现身~~ word2003转成xml后的文件怎样在IE中正常显示阿,我比较弱大家别笑话 C#监听其它进程的TCP数据(在已知进程ID、端口号等情况下) 语音提示(帮忙!!!剧给分) 今天发工资,高兴!!!送分! 大家说说,你们在。NET中实现GIF动画,是用什么做的? panel 滚动 问题!!!
private string GetHtmlSource(string url)
{
string html = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("GB2312"));
html = reader.ReadToEnd();
}
catch
{
MessageBox.Show("无法连接到远程服务器,请检查您的网络是否正常!", "提示");
}
return html; }
#endregion
http://www.cnblogs.com/skyiv/archive/2005/10/01/GetIP.html这里有“获取网页的HTML内容”的代码。
http://www.whitepages.com/5116/search/FindPerson?firstname=&name=wa&name_begins_with=1&city_zip=+01002&state_id=All+US&x=53&y=16
//string src = GetHtmlSource("http://www.whitepages.com/5116/search/Replay?search_id=40051390946113532971");
string src = GetHtmlSource("http://www.whitepages.com/5116/search/FindPerson?firstname=&name=wa&name_begins_with=1&city_zip=+01002&state_id=All+US&x=53&y=16");
MatchCollection mc = Regex.Matches(src, @"<div\s+class=""description"">\s*<h2><a[^>]*>(?<name>[\s\S]*?)</a>\s*</h2>\s*<p>(?<adress>[\s\S]*?)</p>\s*<p>(?<code>[\s\S]*?)</p>\s*<p[^>]*>(?<phone>[\s\S]*?)</p>", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["name"].Value + "\n";
richTextBox2.Text += m.Groups["adress"].Value + "\n";
richTextBox2.Text += m.Groups["code"].Value + "\n";
richTextBox2.Text += m.Groups["phone"].Value + "\n\n";
}private string GetHtmlSource(string url)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream resStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(resStream, System.Text.Encoding.UTF8);
string htmlSource = (sr.ReadToEnd());
resStream.Close();
sr.Close();
return htmlSource;
}