如何处理 VBA 中 HTML 变量的 MailChimp API 响应?

How do I handle the MailChimp API response for the HTML variable in VBA?

字符串变量 oldHTMLContent 包含来自 MailChimp API 请求响应的文本字符串,表示电子邮件活动的当前内容。这是字符串,但它包含一堆您在下面的显示中看不到的 \r\n:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <style type="text/css">
      @media only screen and (max-width: 480px) {
        table#canspamBar td {
          font-size:14px !important;
        }
        
        table#canspamBar td a {
          display:block !important;
          margin-top:10px !important;
        }
      }
    </style>
  </head>
  <body>
    <p> </p>
    <div class="userBot">
      <a href="http://dev.mydev.org/why-so-many-people-are-signing-up-for-cynthia-for-new-york-volunteer-events"><img src="http://dev.mydev.org/wp-content/uploads/2018/07/CynthiaNixon.jpg" width="1012" height="592" alt="CynthiaNixon.jpg"></a>
      <p>When we ask ourselves why so many people are signing up for Cynthia For New York volunteer events this weekend, this is what ... (click for more)</p>
    </div>            <center>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <table border="0" cellpadding="0" cellspacing="0" width="100%" id="canspamBarWrapper" style="background-color:#FFFFFF;border-top:1px solid #E5E5E5;">
      <tr>
        <td align="center" valign="top" style="padding-top:20px;padding-bottom:20px;">
          <table border="0" cellpadding="0" cellspacing="0" id="canspamBar">
            <tr>
              <td align="center" valign="top" style="color:#606060;font-family:Helvetica, Arial, sans-serif;font-size:11px;line-height:150%;padding-right:20px;padding-bottom:5px;padding-left:20px;text-align:center;">
                This email was sent to <a href="mailto:*|EMAIL|*" target="_blank" style="color:#404040 !important;">*|EMAIL|*</a>
                <br><a href="*|ABOUT_LIST|*" target="_blank" style="color:#404040 !important;"><em>why did I get this?</em></a>    <a href="*|UNSUB|*" style="color:#404040 !important;">unsubscribe from this list</a>    <a href="*|UPDATE_PROFILE|*" style="color:#404040 !important;">update subscription preferences</a>
                <br>*|LIST:ADDRESSLINE|*
                <br>
                <br>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </center>
</body>
</html>

我只想提取 "userBot" class 但我似乎无法使用 getElementsByClassName 访问它。

执行此代码时,结果始终为零。

Dim oldHTMLContent As String
Dim oldHtmlDoc As MSHTML.HTMLDocument
Set oldHtmlDoc = New HTMLDocument
oldHtmlDoc.body.innerText=oldHTMLContent
debug.Print oldHtmlDoc.getElementsByClassName("userBot").length

如何定义正确的对象并使用 HTML 字符串加载它以便我可以使用 userBot class?我可以看到我正在加载整个 DOM,包括

作为.innerHTML转移到新的HTMLDocument然后使用CSSclass选择器,".",如下所示。另外,您的命名似乎有些混乱。 IMO 如果你将 oldInnerHTML 转移到 newHTMLDoc 或类似的东西会更清楚。

Option Explicit
Public Sub test()
    Dim html As New HTMLDocument

    html.body.innerHTML = [A1] '<= This is your oldHTMLContent. I am reading from a cell.
    Debug.Print html.querySelector(".userBot").innerText
End Sub

这等于说:

Debug.Print html.getElementsByClassName("userBot")(0).innerText

输出样本: