代码之家  ›  专栏  ›  技术社区  ›  Jimmy Collins luisr

从以下代码中获取“href”值?

  •  0
  • Jimmy Collins luisr  · 技术社区  · 15 年前

    我需要从HTML中获取Href值,如下C_所示:

    <td class="tl"><a href="http://facebook.com/"target="_blank"><img src="images/poput_icon.png"/></a>
    

    有人能告诉我怎么做吗?Regex是最好的方法吗?我需要从一个包含100个链接的页面中收集这些链接,但是它们看起来都像上面的代码。我想忽略页面上的其他href。

    事先谢谢。

    吉米

    2 回复  |  直到 15 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    more detailed information on the whys and wherefores

    LINQ-to-XML XDocument td

    var href = doc
        .Element("td")
        .Element("a")
        .Attribute("href")
        .Value;
    
        2
  •  1
  •   Adam Norberg    15 年前

    Regex document

    Regex linkscraper = new Regex(@"<\s*td[^>]*>\s*<\s*a[^>]*href\s*=\s*""(?<link>[^""]*)""[^>]>\s*<\s*img[^>]*>\s*<\s*\/a\s*>");
    MatchCollection links = linkscraper.matches(document);
    

    Match

    • \s*
    • [^>]
      • [^""] [^"]

    [^>]* <TD><A href=...><IMG></a>

    推荐文章