【问题标题】:Select href tags from string [duplicate]从字符串中选择href标签[重复]
【发布时间】:2018-07-03 14:46:22
【问题描述】:

我有一个 HTML 字符串变量:

 <html>
    <body style="margin: 0; background: #e0e0e0; text-align:center;">
        <div style="padding: 0; text-align: left; width: 800px; margin: 0 auto; background: #e0e0e0; ">
            <div style="text-align: center; border-bottom: 10px solid #000; background: #FEDC00; ">
            </div>
            <div style="border: 0; padding: 20px; width: 760px; background: #fff; font-family: Helvetica, Arial; font-size: 12px;">
               </tr></table><hr /><div style="font-weight: bold; width: 45%; text-align: center; float: left;  "> <a style="font-size: 17px;display: block; color: #fff; background: #009900; text-decoration: none; padding: 10px 20px;" href="dabb68d353e3a82315ec6419e4aaa854&url=booking|358137|b4e3bea97e18db09d7878a6010b5b57b">ACCEPT</a></div>
       </div></div>
    </body>
</html>

如何获取 href 内容?

【问题讨论】:

  • Obligatory。使用旨在解析 HTML 的工具(例如 HtmlAgilityPack),而不是 RegEx。
  • 我建议 stackoverflow.com/questions/15926142/… 重复,因为同时给出了正则表达式(带有工作的正则表达式!)和 HTML 解析解决方案。首选HTML解析解决方案。
  • href=("|')(.*?("|')) 使用这个正则表达式

标签: c# regex


【解决方案1】:

您可以使用 HTMLAgilityPack

var docs = new HtmlAgilityPack.HtmlDocument();
docs.LoadHtml("YourHTML"); 
string HrefValue= docs.DocumentNode.SelectSingleNode("//a").Attributes["href"].Value;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 2015-09-17
    • 2023-02-26
    • 2014-04-04
    • 2013-07-10
    • 1970-01-01
    相关资源
    最近更新 更多