抓取指定div里面的所有链接

大雷神 2011-12-06 04:36:20
<div class="NewsList">
<ul class="c_l14_01" id="impoLisTop01">
<li>
<a href="http://www.baidu.com" target="_blank">标题一</a>
<a href="http://www.163.com" target="_blank">标题二</a>
</li>

<li>
<a href="http://www.baidu.com"target="_blank">标题1</a>
<a href="http://www.163.com" target="_blank">标题2</a>
<a href="http://www.163.com" target="_blank">标题3</a>
</li>
</ul>
<ul class="c_l14_01" id="Ul1">
<li>
<a href="http://www.baidu.com" target="_blank">标题4</a>
<a href="http://www.163.com" target="_blank">标题5</a>
</li>

<li>
<a href="http://www.baidu.com" target="_blank">标题6</a>
<a href="http://www.163.com" target="_blank">标题7</a>
<a href="http://www.163.com" target="_blank">标题8</a>
</li>
</ul>
</div>

//获取指定div:NewsList中的所有标题和链接地址 小弟第一次接触网络拔取 请赐教 主要是正则匹配那块

...全文
361 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
大雷神 2011-12-07
  • 打赏
  • 举报
回复
<div class="NewsList">
<ul>
<li>
<a href="http://www.baidu.com" target="_blank">标题一</a>
<a href="http://www.163.com" target="_blank">标题二</a>
</li>
</ul>

<div class="line_01"></div> //多了这个为什么就抓不到下面的标题和链接了

<ul>
<li>
<a href="http://www.baidu.com" target="_blank">标题6</a>
<a href="http://www.163.com" target="_blank">标题7</a>
<a href="http://www.163.com" target="_blank">标题8</a>
</li>
</ul>
</div>
huangwenquan123 2011-12-06
  • 打赏
  • 举报
回复
            string str = File.ReadAllText(@"E:\a.txt", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"(?is)(?<=<div[^>]*?class=""NewsList""[^>]*?>(?:(?!</?div).)*)<a[^>]*?href=(['""\s]?)(?<href>[^'""\s]+)\1[^>]*?>(?<txt>.*?)</a>");
foreach (Match m in reg.Matches(str))
Console.WriteLine("{0}=={1}", m.Groups["href"].Value, m.Groups["txt"].Value);
/*
http://www.baidu.com==标题一
http://www.163.com==标题二
http://www.baidu.com==标题1
http://www.163.com==标题2
http://www.163.com==标题3
http://www.baidu.com==标题4
http://www.163.com==标题5
http://www.baidu.com==标题6
http://www.163.com==标题7
http://www.163.com==标题8
*/
  • 打赏
  • 举报
回复
 string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));
//获取固定NewsList为div的内容
tempStr = Regex.Match(tempStr, @"<div[^>]+class=(['""]?)NewsList\1[^>]*>[\s\S]+</div>").Value;
//获取div链接
string pattern = @"<a[^>]*href=(""(?<href>[^""]*)""|'(?<href>[^']*)'|(?<href>[^\s>]*))[^>]*>(?<text>[\s\S]*?)</a>";
foreach (Match m in Regex.Matches(tempStr, pattern))
{
//循环输出
string matchtext = m.Value;
string href = m.Groups["href"].Value;// 这是href内容
string text = m.Groups["text"].Value;// 这是text内容,就是<a>这里的内容</a>


}
ruanwei1987 2011-12-06
  • 打赏
  • 举报
回复
抓取指定div里面的所有链接
你题目是 抓取,

你是想实现网络爬虫?
  • 打赏
  • 举报
回复
 string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));
string pattern = @"<a[^>]*href=(""(?<href>[^""]*)""|'(?<href>[^']*)'|(?<href>[^\s>]*))[^>]*>(?<text>[\s\S]*?)</a>";
foreach (Match m in Regex.Matches(tempStr, pattern))
{
//循环输出
string matchtext = m.Value;
string href = m.Groups["href"].Value;// 这是href内容
string text = m.Groups["text"].Value;// 这是text内容,就是<a>这里的内容</a>


}
mchaha123 2011-12-06
  • 打赏
  • 举报
回复
同求。。。。。。。。。。。。。。。
一起得瑟 2011-12-06
  • 打赏
  • 举报
回复
l路过 帮顶
大雷神 2011-12-06
  • 打赏
  • 举报
回复
后台的方法我已经完善了 就是开头难 得不到数据源
private string GetSourceCode(string url) {
WebRequest request = WebRequest.Create(url);//指定要抓取网页的网址
WebResponse response = request.GetResponse(); //得到响应的值
StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
string html = reader.ReadToEnd();
//先停止读再停止响应
reader.Close();
reader.Dispose();
response.Close();
return html;
}

protected void BtnSearch_Click(object sender, EventArgs e)
{
string source = GetSourceCode("http://news.sina.com.cn/");//指定新浪网
Regex regex = new Regex("<a href=\"(?<url>.*?)\" target='_blank' >(?<title>.*?)</a>", RegexOptions.Multiline | RegexOptions.IgnoreCase); //就是这个位置不会写了
//分组捕获url链接以及对应的标题,一个列表页中有多个网页链接
MatchCollection matches = regex.Matches(source); //获取符合要求的数据
for (int i = 0; i<matches.Count;i++ )
{
//启用多线程
Thread thread = new Thread(new ThreadStart(AddInfo));
thread.Name = matches[i].Groups["http://news.sina.com.cn/"].Value;
thread.Start();
}



}

62,046

社区成员

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

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

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

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