如何用正则表达式提取网页内容

HTpaojiamen 2012-03-21 11:27:03
如何用正则表达式提取网页内容
代码如下:<div id="title" class="blog_tit_cont">
<strong>

<span >


<span>[转]</span>
为了练好口语,你敢不敢每天读一遍,坚持一个月?
</span>


</strong>
<span id="pubTime" class="c_tx3">
<script type="text/javascript">
var pubtime = g_oBlogData.data.pubtime;
var pubDate = new Date(pubtime * 1000);
document.write(pubDate.getFullYear() + "." + (pubDate.getMonth() + 1) + "." + pubDate.getDate());
</script>
</span>
<span id="readNum" class="c_tx3"> </span>
<span id="quoteInfo" class="c_tx3"> </span>
</div>




如何提取div下的strong的内容?求详细源码
...全文
104 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2012-03-21
  • 打赏
  • 举报
回复
来个完整点的

C盘建一个1.txt
C# code

<div id="title" class="blog_tit_cont">
<strong>

<span >


<span>[转]</span>
为了练好口语,你敢不敢每天读一遍,坚持一个月?
</span>


</strong>
<span id="pubTime" class="c_tx3">
<script type="text/javascript">
var pubtime = g_oBlogData.data.pubtime;
var pubDate = new Date(pubtime * 1000);
document.write(pubDate.getFullYear() + "." + (pubDate.getMonth() + 1) + "." + pubDate.getDate());
</script>
</span>
<span id="readNum" class="c_tx3"> </span>
<span id="quoteInfo" class="c_tx3"> </span>
</div>


string strfromtxt = File.ReadAllText(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
List<string> pro = new List<string>();
MatchCollection matches1 = Regex.Matches(strfromtxt, @"(?is)<div\s*id=""title""\s*class=""blog_tit_cont"">\s*<strong>(?<strong>(.*))</strong>(.*?)</div>", RegexOptions.IgnoreCase);
foreach (Match match in matches1)
{
string contemp = match.Groups["strong"].Value;
pro.Add(contemp);
}
EnForGrass 2012-03-21
  • 打赏
  • 举报
回复
C盘建一个1.txt

<div id="title" class="blog_tit_cont">
<strong>

<span >


<span>[转]</span>
为了练好口语,你敢不敢每天读一遍,坚持一个月?
</span>


</strong>
<span id="pubTime" class="c_tx3">
<script type="text/javascript">
var pubtime = g_oBlogData.data.pubtime;
var pubDate = new Date(pubtime * 1000);
document.write(pubDate.getFullYear() + "." + (pubDate.getMonth() + 1) + "." + pubDate.getDate());
</script>
</span>
<span id="readNum" class="c_tx3"> </span>
<span id="quoteInfo" class="c_tx3"> </span>
</div>


string strfromtxt = File.ReadAllText(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
List<string> pro = new List<string>();
MatchCollection matches1 = Regex.Matches(strfromtxt, @"(?is)<strong>(?<strong>(.*))</strong>", RegexOptions.IgnoreCase);
foreach (Match match in matches1)
{
string contemp = match.Groups["strong"].Value;
pro.Add(contemp);//结果数据
}
porschev 2012-03-21
  • 打赏
  • 举报
回复


static void Main(string[] args)
{
string str = @"<div id=""title"" class=""blog_tit_cont"">
<strong>

<span >


<span>[转]</span>
为了练好口语,你敢不敢每天读一遍,坚持一个月?
</span>


</strong>
<span id=""pubTime"" class=""c_tx3"">
<script type=""text/javascript"">
var pubtime = g_oBlogData.data.pubtime;
var pubDate = new Date(pubtime * 1000);
document.write(pubDate.getFullYear() + ""."" + (pubDate.getMonth() + 1) + ""."" + pubDate.getDate());
</script>
</span>
<span id=""readNum"" class=""c_tx3""> </span>
<span id=""quoteInfo"" class=""c_tx3""> </span>
</div>
";


Regex re = new Regex(@"(?is)(?<=<div id=""title""[^>]+>\s*<strong>).*?(?=</strong>)", RegexOptions.None);
Console.WriteLine(re.Match(str).Value); //re.Match(str).Value就是你要的
Console.ReadLine();
}

-过客- 2012-03-21
  • 打赏
  • 举报
回复
try...

            Regex reg = new Regex(@"(?is)<div[^>]*>(?:(?!</?div).)*(<strong[^>]*>.*?</strong>)");
MatchCollection mc = reg.Matches(yourStr);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups[1].Value + "\n";
}
EnForGrass 2012-03-21
  • 打赏
  • 举报
回复
(?is)<strong>(?<strong>(.*))</strong>

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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