【问题标题】:Retrive the Url from an Html Img Tag从 Html Img 标签中检索 URL
【发布时间】:2016-07-14 14:11:30
【问题描述】:

背景信息

目前正在开发一个 C# web api,它将返回选定的 Img url 作为 base64。我目前具有执行base64转换的功能,但是,我得到了大量文本,其中还包括Img Url,我需要从字符串中裁剪出来并将其提供给我的函数以将img转换为base 64。我阅读了一个 lib.("HtmlAgilityPack;") ,它应该使这项任务变得容易,但是当我使用它时,我得到 "HtmlDocument.cs" 找不到。但是,我不是在提交文档,而是向它发送一个 HTML 字符串。我阅读了文档,它应该也可以使用字符串,但它不适用于我。这是使用“HtmlAgilityPack”的代码。

非工作代码

foreach(var item in returnList)
                    {
                         if (item.Content.Contains("~~/picture~~"))
                        {
                            HtmlDocument doc = new HtmlDocument();
                            doc.Load(item.Content);

来自 HtmlAgilityPack 的错误消息

问题 我从 SharePoint 收到一个 Html 字符串。这个 Html 字符串可以用标题标记和/或图片标记进行标记。我试图从 img src Hmtl 标记中分离出检索 html。我知道正则表达式可能不切实际,但我会考虑使用正则表达式是否可以从 img src 检索 url。

示例字符串

Bullet~~Increased Cash Flow</li><li>~~/Document Text Bullet~~Tax Efficient Organizational Structures</li><li>~~/Document Text Bullet~~Tax Strategies that Closely Align with Business Strategies</li><li>~~/Document Text Bullet~~Complete Knowledge of State and Local Tax Obligations</li></ul><p>~~/Document Heading 2~~is the firm of choice</p><p>~~/Document Text~~When it comes to accounting and advisory services is the unique firm of choice. As a trusted advisor to our clients, we bring an integrated client service approach with dedicated industry experience. Dixon Hughes Goodman respects the value of every client relationship and provides clients throughout the U.S. with an unwavering commitment to hands-on, personal attention from our partners and senior-level professionals.</p><p>~~/Document Text~~of choice for clients in search of a trusted advisor to deal with their state and local tax needs. Through our leading best practices and experience, our SALT professionals offer quality and ease to the client engagement. We are proud to provide highly comprehensive services.</p>

    <p>~~/picture~~<br></p><p> 
          <img src="/sites/ContentCenter/Graphics/map-al.jpg" alt="map al" style="width&#58;611px;height&#58;262px;" />&#160;
    <br></p><p><br></p><p>
    ~~/picture~~<br></p><p>
          <img src="/sites/ContentCenter/Graphics/Firm_Telescope_Illustration.jpg" alt="Firm_Telescope_Illustration.jpg" style="margin&#58;5px;width&#58;155px;height&#58;155px;" />    </p><p></div><div class="ExternalClassAF0833CB235F437993D7BEE362A1A88A"><br></div><div class="ExternalClassAF0833CB235F437993D7BEE362A1A88A"><br></div><div class="ExternalClassAF0833CB235F437993D7BEE362A1A88A"><br></div>

重要

我正在处理 HTML 字符串,而不是文件。

【问题讨论】:

  • 究竟是什么错误?
  • 虽然我无法回答您的问题,但您可能会对这个问题感兴趣:stackoverflow.com/questions/4257359/…
  • @nbokmans +1 谢谢
  • 该错误消息似乎是说您的解决方案中缺少一个 cs 文件。与敏捷包无关
  • 对,但我使用的是字符串而不是文件

标签: c# string html-agility-pack


【解决方案1】:

您遇到的问题是 C# 正在寻找一个文件,因为它没有找到它,它会告诉您。这不是会阻止您的应用程序的错误,它只是告诉您找不到文件并且 Lib 将读取给定的字符串。可以在此处找到此文档https://htmlagilitypack.codeplex.com/SourceControl/latest#Trunk/HtmlAgilityPackDocumentation.shfbproj。下面的代码是一个任何人都可以使用的千篇一律的模型。

重要

C# 正在寻找一个无法显示的文件,因为它是一个提供的字符串。那是您收到的消息,但是您仍然可以根据提供的文档正常工作,并且不会影响您的代码。

示例代码

HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
htmlDocument.LoadHtml("YourContent"); // can be a string or can be a path.

HtmlAttribute att = url.Attributes["src"];
Uri imgUrl = new System.Uri("Url"+ att.Value); // build your url

【讨论】:

    【解决方案2】:
    string matchString = Regex.Match(original_text, "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value;
    

    已被多次询问here

    还有here

    【讨论】:

    • 对,但这个问题与正则表达式解决方案无关。
    猜你喜欢
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2014-05-23
    相关资源
    最近更新 更多