日期:2014-05-17  浏览次数:21073 次

新闻抓取问题
WebClient wc = new WebClient();
string reg = @"<div\s+class=""content""\s+title="".+?"">(.+?)</div>";
  using (Stream stream = wc.OpenRead("http://www.qiushibaike.com/"))
  {
  using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
  {
  string content = sr.ReadToEnd();
  //单行模式
  //Console.WriteLine(content);
  MatchCollection mc = Regex.Matches(content,reg, RegexOptions.Singleline);
  foreach (Match match in mc)
  {
  if (match.Success)
  {
  Console.WriteLine(match.Groups[1].Value);
  }
  }
  }
  }
为什么不显示呢,求大神相助谢谢

------解决方案--------------------
if (match.Success)
这个判断用不到
------解决方案--------------------
//抓取整个网站的方法
private string Get(string Url)
{
//
string Str = string.Empty;
try
{
WebRequest req = WebRequest.Create(Url);
WebResponse res = req.GetResponse();
StreamReader read = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("gb2312"));
Str = read.ReadToEnd();
read.Close();
read.Dispose();
res.Close();
}
catch
{

}
this.progressBar1.Value = 40;
return Str;
}

//将抓取到的网站进行处理获取到你想要的信息
private string GetTitle(string Str, ref bool Flag)
{
string _str = Str.ToLower();
if (!string.IsNullOrEmpty(Str))
{
try
{
string Title = Str;

_str = _str.Substring(Str.IndexOf("h1title"));
Title = _str.Substring(9, 22);
this.progressBar1.Value = 80;
return Title;
}
catch
{ }
return "";
}
return "";

}

private void button1_Click(object sender, EventArgs e)
{
//点击 数据采集按钮
//进度条 显示
progressBar1.Visible = true;
progressBar1.Value += progressBar1.Step;//让进度条增加一次
System.Threading.Thread.Sleep(50);//暂停1秒

WebRequest Request = WebRequest.Create("http://tech.163.com/12/0911/22/8B5HNUEG000915BF.html");
progressBar1.Value += progressBar1.Step;//让进度条增加一次
WebResponse Response = Request.GetResponse(); //返回 对internet 的响应
progressBar1.Value += progressBar1.Step;//让进度条增加一次
System.Threading.Thread.Sleep(50);//暂停1秒
Stream dataStream = Response.GetResponseStream();
progressBar1.Value += progressBar1.Step;//让进度条增加一次
System.Threading.Thread.Sleep(50);//暂停1秒
StreamReader reader = new StreamReader(dataStream, Encoding.Default);
progressBar1.Value += progressBar1.Step;//让进度条增加一次
System.Threading.Thread.Sleep(50);//暂停1秒
string responseFromServer = reader.ReadToEnd();
progressBar1.Value += progressBar1.Step;//让进度条增加一次