string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt string pattern = @"(?i)<span[^>]*?class=(['""]?)list-identifier\1[^>]*?><a[^>]*?>[^<>\d]*?(?<ID>[\d\.]+)\s*?</a>"; pattern += @"[\s\S]*?<div[^>]*?class=(['""]?)list-title\2[^>]*?>[\s\S]*?<span[^>]*?>[\s\S]*?</span>(?<Title>[\s\S]*?)\s*?</div>"; pattern += @"[\s\S]*?<div[^>]*?class=(['""]?)list-authors\3[^>]*?>[\s\S]*?<span[^>]*?>[\s\S]*?</span>(?:\s*?<a[^>]*?>(?<Authors>[^<>]*?)</a>[^<>]*)+\s*?</div>"; pattern += @"[\s\S]*?<p>\s*?(?<Content>[\s\S]*?)\s*?</p>"; foreach (Match m in Regex.Matches(tempStr, pattern)) { //循环输出 string ID = m.Groups["ID"].Value;//1305.0262 string Title = m.Groups["Title"].Value;//On-sky characterisation of the VISTA NB118 narrow-band filters at 1.19 micron string Authors = string.Join("|", m.Groups["Authors"].Captures.Cast<Capture>().Select(a =>a.Value));// /*不同作者以|分割 * B. Milvang-Jensen|W. Freudling|J. Zabl|J. P. U. Fynbo|P. Moller|K. K. Nilsson|H. Joy McCracken|J. Hjorth|O. Le Fevre|L. Tasca|J. S. Dunlop|D. Sobral */ string Content = m.Groups["Content"].Value;// }
C#使用正则表达式提取网页中的信息数据 http://blog.csdn.net/aa466564931/article/details/6575683
源代码挺长的 里面有几十篇文章,这个网页http://arxiv.org/list/astro-ph/new 右键查看源代码可以看到。想把这些文章的 上述信息保存在一个数组里面,然后再存入数据库
string pattern = @"(?i)<span[^>]*?class=(['""]?)list-identifier\1[^>]*?><a[^>]*?>[^<>\d]*?(?<ID>[\d\.]+)\s*?</a>";
pattern += @"[\s\S]*?<div[^>]*?class=(['""]?)list-title\2[^>]*?>[\s\S]*?<span[^>]*?>[\s\S]*?</span>(?<Title>[\s\S]*?)\s*?</div>";
pattern += @"[\s\S]*?<div[^>]*?class=(['""]?)list-authors\3[^>]*?>[\s\S]*?<span[^>]*?>[\s\S]*?</span>(?:\s*?<a[^>]*?>(?<Authors>[^<>]*?)</a>[^<>]*)+\s*?</div>";
pattern += @"[\s\S]*?<p>\s*?(?<Content>[\s\S]*?)\s*?</p>";
foreach (Match m in Regex.Matches(tempStr, pattern))
{
//循环输出
string ID = m.Groups["ID"].Value;//1305.0262
string Title = m.Groups["Title"].Value;//On-sky characterisation of the VISTA NB118 narrow-band filters at 1.19 micron
string Authors = string.Join("|", m.Groups["Authors"].Captures.Cast<Capture>().Select(a =>a.Value));//
/*不同作者以|分割
* B. Milvang-Jensen|W. Freudling|J. Zabl|J. P. U. Fynbo|P. Moller|K. K. Nilsson|H. Joy McCracken|J. Hjorth|O. Le Fevre|L. Tasca|J. S. Dunlop|D. Sobral
*/
string Content = m.Groups["Content"].Value;//
}
从字符串操作xml不是很简单的事么?XmlDocument.LoadXml 方法
不知道是不是因为存在string里面的不仅仅有这个网页的源代码,前面还有些别的东西
就没有保存成功,就放弃用dom提取了,改用正则了
1、文章所属的类别:subject 也需要提取出来,只提取第一个所属的类别就行,因为还得在界面中通过文章类别查询。这是源代码部分的截图
2、页面中有70多篇文章,现在可以抓取20多篇文章的信息存入数据库了,还有一些不能存储,下面这些程序运行时 ,提示错误的截图:
应该还是你的SQL语句的问题,注意单引号什么的
string tempStr = File.ReadAllText(@"C:\Users\myx\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt
string pattern = @"(?i)<span[^>]*?class=(['""]?)list-identifier\1[^>]*?><a[^>]*?>[^<>\d]*?(?<ID>[\d\.]+)\s*?</a>";
pattern += @"[\s\S]*?<div[^>]*?class=(['""]?)list-title\2[^>]*?>[\s\S]*?<span[^>]*?>[\s\S]*?</span>(?<Title>[\s\S]*?)\s*?</div>";
pattern += @"[\s\S]*?<div[^>]*?class=(['""]?)list-authors\3[^>]*?>[\s\S]*?<span[^>]*?>[\s\S]*?</span>(?:\s*?<a[^>]*?>(?<Authors>[^<>]*?)</a>[^<>]*)+\s*?</div>";
pattern += @"[\s\S]*?<div[^>]*?class=(['""]?)list-subjects\4[^>]*?>[\s\S]*?<span[^>]*?class=(['""]?)primary-subject\5[^>]*?>(?<Subject>[\s\S]*?)</span>[\s\S]*?</div>";
pattern += @"[\s\S]*?<p>\s*?(?<Content>[\s\S]*?)\s*?</p>";
foreach (Match m in Regex.Matches(tempStr, pattern))
{
//循环输出
string ID = m.Groups["ID"].Value;//1305.0262
string Title = m.Groups["Title"].Value;//On-sky characterisation of the VISTA NB118 narrow-band filters at 1.19 micron
string Authors = string.Join("|", m.Groups["Authors"].Captures.Cast<Capture>().Select(a =>a.Value));//
/*不同作者以|分割
* * B. Milvang-Jensen|W. Freudling|J. Zabl|J. P. U. Fynbo|P. Moller|K. K. Nilsson|H. Joy McCracken|J. Hjorth|O. Le Fevre|L. Tasca|J. S. Dunlop|D. Sobral
*/
string Subject = m.Groups["Subject"].Value;//Instrumentation and Methods for Astrophysics (astro-ph.IM)
string Content = m.Groups["Content"].Value;//
}