关于数据采集和正则匹配问题

hbxgyyw 2012-10-19 02:13:32
// 这里URL=http://www.cz88.net/proxy/index.aspx
public static string GetHtml(string URL)
{
WebRequest wr;
wr = WebRequest.Create(URL);
wr.Credentials = CredentialCache.DefaultCredentials;
WebResponse wp;
wp = wr.GetResponse();

string html = new StreamReader(wp.GetResponseStream(), Encoding.UTF8).ReadToEnd();
return html;
}


获取了HTML 就是网页源码 现在的问题就是:我想截取上面的 代理IP和端口
格式就是 xxx.xxx.xxx.xxx:8080
xxx.xxx.xxx.xxx:8080
很多条 然后写入TXT文本

...全文
91 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2012-10-19
  • 打赏
  • 举报
回复
foreach (Match m in Regex.Matches(html, @"\d+(\.\d+){3}:\d+"))
hbxgyyw 2012-10-19
  • 打赏
  • 举报
回复
private void button1_Click(object sender, EventArgs e)
{
Regex reg;
string html=GetHtml("http://www.cz88.net/proxy/index.aspx");
foreach (Match m in reg.Matches(html, @"\d+(\.\d+){3}:\d+"))
//这里报错请改用类型名来限定它
{
Ctxt(m.Value);
}
}
/// <summary>
/// 创建文本并写入内容
/// </summary>
/// <param name="content"></param>
public void Ctxt(string content)
{
string file = "tt.txt";
//string content = txtContent.Text;//内容
if (!File.Exists(file) == true)
{
MessageBox.Show("存在此文件!");
}
else
{
FileStream myFs = new FileStream(file, FileMode.Create);
StreamWriter mySw = new StreamWriter(myFs);
mySw.Write(content);
mySw.Close();
myFs.Close();
MessageBox.Show("写入成功");
}
}

/// <summary>
/// 采集函数
/// </summary>
/// <param name="URL">地址</param>
/// <returns>返回采集到的HTML</returns>
public string GetHtml(string URL)
{
WebRequest wr;
wr = WebRequest.Create(URL);
wr.Credentials = CredentialCache.DefaultCredentials;
WebResponse wp;
wp = wr.GetResponse();

string html = new StreamReader(wp.GetResponseStream(), Encoding.UTF8).ReadToEnd();

return html;
}

诺维斯基 2012-10-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

\d+(\.\d+){3}:\d+
[/Quote]
可以了。
q107770540 2012-10-19
  • 打赏
  • 举报
回复
\d+(\.\d+){3}:\d+

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧