日期:2014-05-18 浏览次数:20713 次
void Main() { string s="<A title=\"豆油上涨步伐或将放缓\" href=\"zixun/InfoList.asp?id=9164\" target=_blank>豆油上涨步伐或将放缓</A>"; Match m=Regex.Match(s,@"(?is)<a\stitle=""豆油上涨步伐或将放缓""\shref=""([^>].*?)""\s.*?>.*?</a>"); if(m.Success) { Console.WriteLine(m.Groups[1].Value); } } //zixun/InfoList.asp?id=9164
------解决方案--------------------
先添加一个webbrowser控件
public partial class Form1 : Form { public Form1() { InitializeComponent(); this.webBrowser1.Navigate(textBox1.Text); } private void button1_Click(object sender, EventArgs e) { if (webBrowser1.Document != null) { HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("a"); foreach (HtmlElement elem in elems) { String nameStr = elem.GetAttribute("title"); if (nameStr == textBox2.Text) { string hrefStr = elem.GetAttribute("href"); MessageBox.Show(hrefStr); break; } } } else MessageBox.Show("网页加载出错"); } }