“<span class="zg_hrsr_ladder">- <a href="http://www.amazon.cn/gp/bestsellers/books/ref=pd_zg_hrsr_b_1_1">图书</a> > <a href="http://www.amazon.cn/gp/bestsellers/books/658404051/ref=pd_zg_hrsr_b_1_2">健身与保健</a> > <a href="http://www.amazon.cn/gp/bestsellers/books/658664051/ref=pd_zg_hrsr_b_1_3">常见病预防与治疗</a> > <b><a href="http://www.amazon.cn/gp/bestsellers/books/660189051/ref=pd_zg_hrsr_b_1_4_last">抑郁症与心理治疗</a></b></span>
</li>
<li class="zg_hrsr_item">
<span class="zg_hrsr_rank">第49位</span>
<span class="zg_hrsr_ladder">- <a href="http://www.amazon.cn/gp/bestsellers/books/ref=pd_zg_hrsr_b_2_1">图书</a> > <a href="http://www.amazon.cn/gp/bestsellers/books/658428051/ref=pd_zg_hrsr_b_2_2">医学</a> > <a href="http://www.amazon.cn/gp/bestsellers/books/659044051/ref=pd_zg_hrsr_b_2_3">神经病和精神病学</a> > <b><a href="http://www.amazon.cn/gp/bestsellers/books/661975051/ref=pd_zg_hrsr_b_2_4_last">精神病学</a></b></span>
</li>
<li class="zg_hrsr_item">
<span class="zg_hrsr_rank">第66位</span>
<span class="zg_hrsr_ladder">- <a href="http://www.amazon.cn/gp/bestsellers/books/ref=pd_zg_hrsr_b_3_1">图书</a> > <a href="http://www.amazon.cn/gp/bestsellers/books/658404051/ref=pd_zg_hrsr_b_3_2">健身与保健</a> > <a href="http://www.amazon.cn/gp/bestsellers/books/658671051/ref=pd_zg_hrsr_b_3_3">心理健康</a> > <b><a href="http://www.amazon.cn/gp/bestsellers/books/660212051/ref=pd_zg_hrsr_b_3_4_last">心理治疗</a></b></span>
”
要求结果如下(数组 或者 DataTable 都可以)
图书 健身与保健 常见病预防与治疗 抑郁症与心理治疗
图书 医学 神经病和精神病学 精神病学
图书 健身与保健 心理健康 心理治疗
</li>
<li class="zg_hrsr_item">
<span class="zg_hrsr_rank">第49位</span>
<span class="zg_hrsr_ladder">- <a href="http://www.amazon.cn/gp/bestsellers/books/ref=pd_zg_hrsr_b_2_1">图书</a> > <a href="http://www.amazon.cn/gp/bestsellers/books/658428051/ref=pd_zg_hrsr_b_2_2">医学</a> > <a href="http://www.amazon.cn/gp/bestsellers/books/659044051/ref=pd_zg_hrsr_b_2_3">神经病和精神病学</a> > <b><a href="http://www.amazon.cn/gp/bestsellers/books/661975051/ref=pd_zg_hrsr_b_2_4_last">精神病学</a></b></span>
</li>
<li class="zg_hrsr_item">
<span class="zg_hrsr_rank">第66位</span>
<span class="zg_hrsr_ladder">- <a href="http://www.amazon.cn/gp/bestsellers/books/ref=pd_zg_hrsr_b_3_1">图书</a> > <a href="http://www.amazon.cn/gp/bestsellers/books/658404051/ref=pd_zg_hrsr_b_3_2">健身与保健</a> > <a href="http://www.amazon.cn/gp/bestsellers/books/658671051/ref=pd_zg_hrsr_b_3_3">心理健康</a> > <b><a href="http://www.amazon.cn/gp/bestsellers/books/660212051/ref=pd_zg_hrsr_b_3_4_last">心理治疗</a></b></span>
”
要求结果如下(数组 或者 DataTable 都可以)
图书 健身与保健 常见病预防与治疗 抑郁症与心理治疗
图书 医学 神经病和精神病学 精神病学
图书 健身与保健 心理健康 心理治疗
void Main()
{
string html=@"<span class=""zg_hrsr_ladder"">- <a href=""http://www.amazon.cn/gp/bestsellers/books/ref=pd_zg_hrsr_b_1_1"">图书</a> > <a href=""http://www.amazon.cn/gp/bestsellers/books/658404051/ref=pd_zg_hrsr_b_1_2"">健身与保健</a> > <a href=""http://www.amazon.cn/gp/bestsellers/books/658664051/ref=pd_zg_hrsr_b_1_3"">常见病预防与治疗</a> > <b><a href=""http://www.amazon.cn/gp/bestsellers/books/660189051/ref=pd_zg_hrsr_b_1_4_last"">抑郁症与心理治疗</a></b></span>
</li>
<li class=""zg_hrsr_item"">
<span class=""zg_hrsr_rank"">第49位</span>
<span class=""zg_hrsr_ladder"">- <a href=""http://www.amazon.cn/gp/bestsellers/books/ref=pd_zg_hrsr_b_2_1"">图书</a> > <a href=""http://www.amazon.cn/gp/bestsellers/books/658428051/ref=pd_zg_hrsr_b_2_2"">医学</a> > <a href=""http://www.amazon.cn/gp/bestsellers/books/659044051/ref=pd_zg_hrsr_b_2_3"">神经病和精神病学</a> > <b><a href=""http://www.amazon.cn/gp/bestsellers/books/661975051/ref=pd_zg_hrsr_b_2_4_last"">精神病学</a></b></span>
</li>
<li class=""zg_hrsr_item"">
<span class=""zg_hrsr_rank"">第66位</span>
<span class=""zg_hrsr_ladder"">- <a href=""http://www.amazon.cn/gp/bestsellers/books/ref=pd_zg_hrsr_b_3_1"">图书</a> > <a href=""http://www.amazon.cn/gp/bestsellers/books/658404051/ref=pd_zg_hrsr_b_3_2"">健身与保健</a> > <a href=""http://www.amazon.cn/gp/bestsellers/books/658671051/ref=pd_zg_hrsr_b_3_3"">心理健康</a> > <b><a href=""http://www.amazon.cn/gp/bestsellers/books/660212051/ref=pd_zg_hrsr_b_3_4_last"">心理治疗</a></b></span>
";
List<string> list=new List<string>();
foreach(Match m in Regex.Matches(html,@"(?<=>)((?!(?:- | >|第\d+)).)*?(?=<)",RegexOptions.IgnoreCase))
{
list.Add(m.Value);
}
var result=list.ToArray();
}
MatchCollection matches = Regex.Matches(s, @"(?is)<span[^>]+class=""zg_hrsr_ladder""[^>]*>.*?<a[^>]+>([^<>]+)</a>.*?<a[^>]+>([^<>]+)</a>.*?<a[^>]+>([^<>]+)</a>.*?<a[^>]+>([^<>]+)</a>.*?</span>");
foreach (Match match in matches)
{
Response.Write(match.Groups[1].Value + "<br/>");
Response.Write(match.Groups[2].Value + "<br/>");
Response.Write(match.Groups[3].Value + "<br/>");
Response.Write(match.Groups[4].Value + "<br/>");
}
输出:
图书
健身与保健
常见病预防与治疗
抑郁症与心理治疗
图书
医学
神经病和精神病学
精神病学
图书
健身与保健
心理健康
心理治疗你自己把它整到数组或datatable里去吧。
1楼的代码简化一下:void Main()
{
string html ="your html";
string[] result=Regex.Matches(html,@"(?<=>)((?!(?:- | >|第\d+|\s+)).)*?(?=<)").Cast<Match>().Select(m=>m.Value).ToArray();
}
@"(?isn)<a.+?href=""(?<url>[^""]+)[^>]+>(?<text>.+?)</a>");
foreach (Match m in mc)
{
MessageBox.Show(m.Groups["url"].Value + Environment.NewLine +
m.Groups["text"].Value);获取连接地址和连接文字
}
图书 健身与保健 常见病预防与治疗 抑郁症与心理治疗
图书 医学 神经病和精神病学 精神病学
图书 健身与保健 心理健康 心理治疗
1 楼的在整个网页里提取就有误
MatchCollection mc = Regex.Matches(href2, @"<a\s?href=(?<url>.*?)>(?<content>.*?)</a>");
string s = "";
foreach (Match m in mc)
{
s += m.Groups["content"].Value + "\n";
}
MessageBox.Show(s);
顺便稍微修改下
string str="";
foreach (Match match in matches)
{
str+=match.Groups[1].Value+",";
//Response.Write(match.Groups[2].Value + "<br/>");
//Response.Write(match.Groups[3].Value + "<br/>");
//Response.Write(match.Groups[4].Value + "<br/>");
}
string[] arry=str.split(',');//将得到的放入数组中
string str=网页源码.substring(0,网页源码.Indexof("aaa"));
获取网页源码的方法如下:
//根据url路径获取网页源码
private string GetWebContent(string sUrl)
{
string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
//声明一个HttpWebRequest请求
request.Timeout = 3000000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.ToString() != "")
{
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("UTF-8");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = streamReader.ReadToEnd();
}
}
catch (Exception exp)
{
writeLog(exp.Message, DateTime.Now);
strResult = "";
}
return strResult;
}
string s = File.ReadAllText(Server.MapPath("~/test.txt"));
HtmlDocument xmlDoc = new HtmlDocument();
xmlDoc.LoadHtml(s);
HtmlNodeCollection nodes = xmlDoc.DocumentNode.SelectNodes(@"//div[@class='bucket' and position()>12]/div[@class='content']/ul/li");
foreach (HtmlNode node in nodes)
{
HtmlNodeCollection anchors = node.SelectNodes(@"*");
foreach (HtmlNode anchor in anchors)
Response.Write(anchor.InnerText + "<br/>");
Response.Write("<br/>");
}
图书
健身与保健
常见病预防与治疗
抑郁症与心理治疗图书
健身与保健
心理健康
心理治疗图书
医学
神经病和精神病学
精神病学