<a href="/scholar.ris?q=info:DoghlrTQTeIJ:scholar.google.com/&output=citation&hl=zh-CN&as_sdt=2000&oe=GB&ct=citation&cd=2">导入RefMan</a>
想获取
/scholar.ris?q=info:DoghlrTQTeIJ:scholar.google.com/&output=citation&hl=zh-CN&as_sdt=2000&oe=GB&ct=citation&cd=2但是,各位都是高人,html里面的情况很复杂,我尝试做,但是失败了
MatchCollection mc = Regex.Matches(str, @"<a\s+href=""(/scholar\.ris\?q=[^""]+)[""]>导入RefMan</a>", RegexOptions.IgnoreCase);请高人解答,非常感谢
Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>");
MatchCollection mc = reg.Matches(test);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["url"].Value + "\n";
}
@"<a\s+href=""([^""]+)""[^>]*>导入RefMan</a>"我试了不行网页url:
http://scholar.google.com/scholar?&hl=zh-CN&q=Offsets+of+Parametric+Curves+and+Surfaceshtml代码确实太难搞了
很奇怪,用我截取的部分测试没问题,但是用整个网页的html代码,就是一条也获取不到
郁闷
<a\s+href=""([^""]+)""[^>]*>导入RefMan</a>
也可以啊,对整个源码
如果是webrequest、webclient之类的话可能没有“导入RefMan”的部分,默认没有的
网页url:
http://scholar.google.com/scholar?&hl=zh-CN&q=Offsets+of+Parametric+Curves+and+Surfaces麻烦您了
private string GetHtmlCode(string url, Encoding encoding)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream resStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(resStream, encoding);
string html = (sr.ReadToEnd());
resStream.Close();
sr.Close();
return html;
}
//解析网页,取URL
string html = GetHtmlCode("http://scholar.google.com/scholar?&hl=zh-CN&q=Offsets+of+Parametric+Curves+and+Surfaces", Encoding.UTF8);
Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>");
MatchCollection mc = reg.Matches(html);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["url"].Value + "\n";
}
处理结构比较规范的文本,自然是用XML方式解析快速,也容易让人接受
而不推荐正则的,还有一部分原因是由于正则太抽象了,很多人敬而远之;又有些人自己写的正则不好,却把效率问题归结到正则上XML方式对格式规范程度要求较高,效率高,但不够灵活,对不规范的文本无能为力
正则方式优势在于灵活,但正则写的不好或应用场景不对效率较低不同的方式有不同的应用场景,各有各的优势,谁也取代不了谁正则有点像瑞士军刀,小巧灵活,功能强大,无所不能,但有人偏要一边拿它砍树,一边嘴里嘟囔着:“什么破玩意,这么钝,砍个树慢死了!”
我就只能汗了
<A href=".."> x</a>这样的读取时候就会报错了哦