111,129
社区成员
发帖
与我相关
我的任务
分享using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string GetHtml(string url)
{
HttpWebRequest wq = (HttpWebRequest)WebRequest.Create(url);
wq.Timeout = 1000000;
wq.Credentials = CredentialCache.DefaultCredentials;
wq.Method = "GET";
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)wq.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
string responseText = streamReader.ReadToEnd();
streamReader.Close();
return responseText;
}
catch (WebException ex)
{
MessageBox.Show(ex.Message );
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
if (wq != null)
{
wq.Abort();
wq = null;
}
}
return "";
}
private void button1_Click(object sender, EventArgs e)
{
string strHTML = GetHtml("http://www.baidu.com/s?wd=%B9%B7%C8%D5%B5%C4360");
strHTML = strHTML.Replace("\n", "").Replace("\r", "").Replace("\t", "");
string pattern = "<font size=\"3\">(?<Info>.*?)</table>";
Regex exp = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
MatchCollection matchList = exp.Matches(strHTML);
if (matchList.Count == 0)
return;
for (int j = 0; j < matchList.Count; j++)
{
MessageBox.Show(matchList[j].Groups["Info"].Value);
}
}
}
}