asp.net 怎么抓取一个也面上的所有超链接 假如页面上有很多<li><a href="" ></a></li> 我想获得页面上所有<li><a href="" ></a></li> 对于href里面的地址。我改怎么写, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1.利用HttpWebResponse或者WebClient得到网页源码2.利用正则或者HtmlDocument分析源码正则就是:(?i)<a[^>]*?href=(['""]?)(?<href>[^'""]*?)\1[^>]*?>取分组m.Groups["href"].value 用HtmlAgilityPack解析抓取到的网页源码,取出所有的a元素,获取其href属性的值就行了。取出所有的a元素的xpath表达式为://a示例:输出博客园首页所有超链接的地址。 HttpWebRequest httpWebRequest = WebRequest.Create(@"http://www.cnblogs.com") as HttpWebRequest; HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; Stream stream = httpWebResponse.GetResponseStream(); StreamReader reader = new StreamReader(stream, Encoding.UTF8); string s = reader.ReadToEnd(); reader.Close(); httpWebResponse.Close(); HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(s); HtmlNodeCollection anchors = htmlDoc.DocumentNode.SelectNodes(@"//a"); foreach (HtmlNode anchor in anchors) Response.Write(anchor.Attributes["href"].Value + "<br/>"); 抓去这些我都知道。 主要就是这个正则怎么写 我要herf里面包含一个某个单词的 只要取到href里面数据 还一个问题是 假如我要取某个标签里面的数据改怎么写。 比如title里面的数据 又或者是定义在页面上面js一个参数 比如 aaa="bbb" 我要取aaa 对于的这个bbb正则怎么写 string key = textBox1.Text; //aaaRegex reg = new Regex(Regex.Escape(key) + @"=(['""]?)(?<value>[^'""\s>]*)\1");MatchCollection mc = reg.Matches(yourStr);foreach (Match m in mc){ richTextBox2.Text += m.Groups["value"].Value + "\n"; //bbb} 前台的话可以用JQUERY读取 $("ul li a").each(function(){ alert($(this).attr("href"))}) 我不是要得到textbox的value 我是想得到某个div里面的数据。 你要通过aaa取bbb,那么aaa就应该是个变量,这里只是举例从textbox取这个变量而已拜托找本基础的书看看先 aps.net web开发使用什么报表工具好 Accordion找不到对象错误。。。 关于<img>和js的问题,在线等!!!!!!!! ■■■■■如何把这个文本文件内容导入数据库中??请大家帮帮忙■■■■■■■■■ 2003下遇到的IIS 的麻烦 Lable.ForeColor问题 有关读取下条记录或是上条记录的问题~ 文件下载的地址 问一个关于dropdownlist的问题 System.Data.SqlClient.SqlException: 用户 'QHC\ASPNET' 登录失败 这个导出的文件名称怎么是乱码啊,内容又不是乱码 ExecumentNonQuery()数据库操纵语言
2.利用正则或者HtmlDocument分析源码
正则就是:(?i)<a[^>]*?href=(['""]?)(?<href>[^'""]*?)\1[^>]*?>
取分组m.Groups["href"].value
取出所有的a元素的xpath表达式为://a示例:输出博客园首页所有超链接的地址。
HttpWebRequest httpWebRequest = WebRequest.Create(@"http://www.cnblogs.com") as HttpWebRequest;
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
Stream stream = httpWebResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
string s = reader.ReadToEnd();
reader.Close();
httpWebResponse.Close();
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(s);
HtmlNodeCollection anchors = htmlDoc.DocumentNode.SelectNodes(@"//a");
foreach (HtmlNode anchor in anchors)
Response.Write(anchor.Attributes["href"].Value + "<br/>");
Regex reg = new Regex(Regex.Escape(key) + @"=(['""]?)(?<value>[^'""\s>]*)\1");
MatchCollection mc = reg.Matches(yourStr);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["value"].Value + "\n"; //bbb
}
$("ul li a").each(function(){ alert($(this).attr("href"))
})
你要通过aaa取bbb,那么aaa就应该是个变量,这里只是举例从textbox取这个变量而已
拜托找本基础的书看看先