我怎么能在一个字符串上循环,并得到以jpg结束的href之间的链接

本文关键字:jpg 结束 链接 之间 href 循环 怎么能 字符串 一个 | 更新日期: 2023-09-27 18:04:09

我正在使用webBrowser在完成的事件中导航到一个网站:

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            mshtml.HTMLDocument objHtmlDoc = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
            string pageSource = objHtmlDoc.documentElement.innerHTML;
        }

现在在pageSource中我有整个页面的源代码。我试着让

string[] lines = File.ReadAllLines(pageSource);

但是它给我例外:

路径

中的非法字符

然后我试了这行:

var aContents = Regex.Matches(pageSource, @"<a [^>]*>(.*?)</a>").Cast<Match>().Select(m => m.Groups[1].Value);

但是如果在content

我怎么能在一个字符串上循环,并得到以jpg结束的href之间的链接

中没有三个ref行

使用htmllagilitypack http://html-agility-pack.net

,您可以使用library方法从url -加载,然后检查节点是否包含ext并将其存储在集合中。

 List<string> alljpgHref = new List<string>;
 HtmlWeb hw = new HtmlWeb();
 HtmlDocument doc = hw.Load(/* url */);
 foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
 {
      string hrefValue = link.GetAttributeValue( "href", string.Empty );
      if (hrefValue.contains(".jpg")) alljpgHref.add(hrefValue);
 }

或者直接查询链接:

string[] hrefs = this.webBrowser1.Document.Links.Cast<HtmlElement>()
             .Select(a => a.GetAttribute("href")).Where(h => h.Contains(".jpg")).ToArray();