网页中部分代码如下:
---------------------------------------------------
<div class="item">
<div class="pic">
<a target="_blank" href="http://item.taobao.com/item.htm?id=8121200640">
<img src="http://img01.taobaocdn.com/bao/uploaded/i1/T1AcdRXdxfXXbSw1U._082124.jpg_160x160.jpg">
</a>
</div>
<div class="desc">
<a target="_blank" href="http://item.taobao.com/item.htm?id=8121200640" class="permalink"> 促销包邮经典实拍 </a>
</div>
<div class="price">
<span>一口价</span>
<strong>198.0元</strong>
</div>
</div>
------------------------------------------------------------
求:
图片地址
相应文字描述
价格
------------
昨天求了一次,得到高人相助,交上去后说不行,还要加个价格
---------------------------------------------------
<div class="item">
<div class="pic">
<a target="_blank" href="http://item.taobao.com/item.htm?id=8121200640">
<img src="http://img01.taobaocdn.com/bao/uploaded/i1/T1AcdRXdxfXXbSw1U._082124.jpg_160x160.jpg">
</a>
</div>
<div class="desc">
<a target="_blank" href="http://item.taobao.com/item.htm?id=8121200640" class="permalink"> 促销包邮经典实拍 </a>
</div>
<div class="price">
<span>一口价</span>
<strong>198.0元</strong>
</div>
</div>
------------------------------------------------------------
求:
图片地址
相应文字描述
价格
------------
昨天求了一次,得到高人相助,交上去后说不行,还要加个价格
Match m = Regex.Match(str, pstr);
m.Groups[1].Value; //或m.Result("$1");
m.Groups[2].Value; //或m.Result("$2");
m.Groups[3].Value; //或m.Result("$3");
你如果想要匹配多次
就用foreach不就OK了 foreach(Match m in Regex.Matches(str, pstr))
{
m.Groups[1].Value; //或m.Result("$1");
m.Groups[2].Value; //或m.Result("$2");
m.Groups[3].Value; //或m.Result("$3");
}
抽时间看看这个
http://download.csdn.net/source/2844387
<div class=""pic"">
<a target=""_blank"" href=""http://item.taobao.com/item.htm?id=8121200640"">
<img src=""http://img01.taobaocdn.com/bao/uploaded/i1/T1AcdRXdxfXXbSw1U._082124.jpg_160x160.jpg"">
</a>
</div>
<div class=""desc"">
<a target=""_blank"" href=""http://item.taobao.com/item.htm?id=8121200640"" class=""permalink""> 促销包邮经典实拍 </a>
</div>
<div class=""price"">
<span>一口价</span>
<strong>198.0元</strong>
</div>
</div>"; MatchCollection mc = Regex.Matches(temp, @"(?i)<div class=\""item(?:[^>]*>){3}\s+<img[^>]+?src=\""([^\""]+)\""(?:[^>]*>){5}([^<>]+)(?:[^>]*>){6}([^<>]+)");
foreach (Match m in mc)
{
Console.WriteLine(m.Groups[1]);
Console.WriteLine(m.Groups[2]);
Console.WriteLine(m.Groups[3]);
}
在昨天基础上。修改一点就好,倒数第二个,也就是分组4就是你要的,其他的不变。
{
string html = @"<div class=""item"">
<div class=""pic"">
<a target=""_blank"" href=""http://item.taobao.com/item.htm?id=8121200640"">
<img src=""http://img01.taobaocdn.com/bao/uploaded/i1/T1AcdRXdxfXXbSw1U._082124.jpg_160x160.jpg"">
</a>
</div>
<div class=""desc"">
<a target=""_blank"" href=""http://item.taobao.com/item.htm?id=8121200640"" class=""permalink""> 促销包邮经典实拍 </a>
</div>
<div class=""price"">
<span>一口价</span>
<strong>198.0元</strong>
</div>
</div>";
MatchCollection mc = Regex.Matches(html, @"(?is)<img\s.*?src=(['""]?)(?<url>[^'"" ]+)(?=\1)[^>]*>[^>]*>([^>]*>){3}\s*([^<]+)(?:[^>]*>){6}(\d+(?:\.\d+)?)");
foreach (Match m in mc)
{
Console.WriteLine(m.Groups[3].Value);//说明
Console.WriteLine(m.Groups[4].Value);//价格
Console.WriteLine(m.Groups[5].Value);//图片地址
}
}
private void button1_Click(object sender, EventArgs e)
{
string url = "http://shop62898199.taobao.com/?search=y";
string result = null;
WebClient client = new WebClient();
result = client.DownloadString(url);
Test(result)
}
public void Test(string html)
{
string str = null;
MatchCollection mc = Regex.Matches(html, @"(?is)<img\s.*?src=(['""]?)(?<url>[^'"" ]+)(?=\1)[^>]*>[^>]*>([^>]*>){3}\s*([^<]+)(?:[^>]*>){6}(\d+(?:\.\d+)?)");
foreach (Match m in mc)
{
//Console.WriteLine(m.Groups[3].Value);//说明
//Console.WriteLine(m.Groups[4].Value);//价格
//Console.WriteLine(m.Groups[5].Value);//图片地址
str += m.Groups[3].Value + m.Groups[4].Value + m.Groups[5].Value + "\n";
}
richTextBox1.Text = str;
}
---------------------------------------------------------------
程序崩馈了