C# 如何根据关键词获取html页面的超链接

yuhaichao928 2012-03-20 09:35:03
例如
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head>
<body>
<div id="show">

<a href="www.baidu.com">百度</a>



</div>

</body>
</html>

我根据 百度 这个关键词 获取 www.baidu.com

...全文
253 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yuhaichao928 的回复:]

思路是有的,用正则取出<a href="www.baidu.com">百度</a>,之后在根据a标签的文本在取出地址 只是正则表达式不知道怎么写 楼上的是取出a标签的所有属性
[/Quote]
dicstr中Key是百度,value是www.baidu.com,你先把代码试试
yazhou137263256 2012-03-20
  • 打赏
  • 举报
回复
遍历html标签,然后用正则表达式去匹配a标签,提取文本为百度的 a标签的href属性
bdmh 2012-03-20
  • 打赏
  • 举报
回复

string source = @"<a href=""www.baidu.com"">百度</a>";
Regex reg = new Regex(@"<a href=""(?<web>[^""]+)"">百度");
MatchCollection mc = reg.Matches(source);
foreach (Match m in mc)
{
MessageBox.Show(m.Groups["web"].Value);
}
yuhaichao928 2012-03-20
  • 打赏
  • 举报
回复
思路是有的,用正则取出<a href="www.baidu.com">百度</a>,之后在根据a标签的文本在取出地址 只是正则表达式不知道怎么写 楼上的是取出a标签的所有属性
EnForGrass 2012-03-20
  • 打赏
  • 举报
回复
在C盘新建一个Txt,内容如下

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs"

Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head>
<body>
<div id="show">
<a href="www.baidu.com">百度</a>
<a href="www.sina.com">新浪</a>
<a href="www.google.cn">谷歌</a>
<a href="www.souhu.com">搜狐</a>
</div>
</body>
</html>



Dictionary<string, string> dicstr = new Dictionary<string, string>();
string strfromtxt = File.ReadAllText(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
string res = @"(?is)<a\s*href=""(?<href>([^>]*))""\s*>(?<value>(.*?))</a>";
MatchCollection matches = Regex.Matches(strfromtxt, res);
foreach (Match match in matches)
{
dicstr.Add(match.Groups["value"].Value.Trim(), match.Groups["href"].Value.Trim());//数据结果在dicstr
}
yyl8781697 2012-03-20
  • 打赏
  • 举报
回复
遍历html标签,然后用正则表达式去匹配a标签,提取文本为百度的 a标签的href属性
EnForGrass 2012-03-20
  • 打赏
  • 举报
回复
BS中不知道怎么弄?给你个思路,你可以遍历整个html,找a标签然后把href中内容和被包含的标签值(如百度)存为键值对,可以用正则实现。但是不知道BS读取html是不是要简单一些呢
liuxing19870629 2012-03-20
  • 打赏
  • 举报
回复
用jQuery可以么?

$("a").each(function(){
if($(this).html()=="百度")
{
alert($(this).attr("href"));
}
});
相当之稳重 2012-03-20
  • 打赏
  • 举报
回复
webrequest 对象可以获取网页源码,然后遍历查找字符串

62,267

社区成员

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

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

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

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