C# 正则获取网页内容, 抓取html源代码里的 title
using System.Text.RegularExpressions;
static void Main(string[] args)
{
// 注意文本编码, 建议选择utf-8格式编码, 要不容易出现乱码
string content = System.IO.File.ReadAllText("html源代码.txt");
// 建立正则匹配, 抓取html源代码里的title
string titleReg = "(?<=
)(.*?)(?=)";string title = "";
try
{
// 正则匹配
Match m = Regex.Match(content, titleReg);
if (m.Success)
{
title = m.Value;
}
Console.WriteLine(title);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
“html源代码.txt”示例内容
标题内容