簡體   English   中英

從HTML Img標簽檢索網址

[英]Retrive the Url from an Html Img Tag

背景信息

當前正在使用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字符串,而不是文件。

string matchString = Regex.Match(original_text, "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value;

這里已被多次詢問。

也在這里

您遇到的問題是C#正在尋找文件,並且由於找不到文件,它會告訴您。 這不是會使您的應用程序崩潰的錯誤,它只是告訴您未找到文件,並且Lib會讀取給定的字符串。 可以在以下網址找到此文檔:https://htmlagilitypack.codeplex.com/SourceControl/latest#Trunk/HtmlAgilityPackDocumentation.shfbproj 下面的代碼是任何人都可以使用的cookie切割器模型。

重要

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM