【问题标题】:Extract desired attribute value from HTML string using regex使用正则表达式从 HTML 字符串中提取所需的属性值
【发布时间】:2019-04-17 15:33:08
【问题描述】:

我从 Discourse API 检索到带有一些元素(p, span, div 等)的 HTML 字符串,其中一些具有 data-time, data-timezone, data-email-preview 等属性。我想要属性 data-email-preview 上的值,这些值是时间戳格式enter code here。这些值总是在 HTML 字符串中的前两个 span 元素之间。 HTML 字符串示例:

<p><span data-date="2019-05-10" data-time="19:00:00" class="discourse-local-date" data-timezones="Europe/Brussels" data-timezone="Europe/Berlin" data-email-preview="2019-05-10T17:00:00Z UTC">2019-05-10T17:00:00Z</span> → <span data-date="2019-05-10" data-time="22:00:00" class="discourse-local-date" data-timezones="Europe/Brussels" data-timezone="Europe/Berlin" data-email-preview="2019-05-10T20:00:00Z UTC">2019-05-10T20:00:00Z</span><br>
<div class="lightbox-wrapper"><div class="meta">
<span class="filename">HackSpace_by_Sugar_Ray_Banister.jpg</span><span class="informations">1596×771 993 KB</span><span class="expand"></span>
</div></a></div></p>

我需要提取span 元素之间的这两个日期:

2019-05-10T17:00:00Z2019-05-10T20:00:00Z

【问题讨论】:

标签: html regex extract word


【解决方案1】:

(?&lt;=&gt;)(\d{4}\-\d{2}\-\d{2}T\d{2}\:\d{2}\:\d{2}Z)(?=&lt;\/span&gt;)

会返回您需要的元素

【讨论】:

    【解决方案2】:

    也许这会满足您的需求?

    https://regex101.com/r/Jo4srA/1

    (稍作编辑以满足您的需求)

    【讨论】:

    • 与此相关的几个问题是它还返回来自data-email-preview 的日期,并且不包括Z
    • 添加 Z 字符只需几秒钟。您能否详细说明应该忽略什么以及要捕获什么?
    【解决方案3】:

    在 VBA 中类似的东西

    Sub Extract2()
    
        Dim hDoc As MSHTML.HTMLDocument
        Dim hElem As MSHTML.HTMLGenericElement
        Dim sFile As String, lFile As Long
        Dim pat1 As String
        Dim sHtml As String
            strHtml = "c:\1.html"
                   'read in the file
                    lFile = FreeFile
                    sFile = strDir & strHtml
                    Open sFile For Input As lFile
                    sHtml = Input$(LOF(lFile), lFile)
    
                    'put into an htmldocument object
                    Set hDoc = New MSHTML.HTMLDocument
                    hDoc.body.innerHTML = sHtml
    
                    Set dateBody = hDoc.getElementsByClassName("discourse-local-date")
                    Date1 = dateBody(0).innerText
                    Date2 = dateBody(1).innerText
                        MsgBox Date1 & " " & Date2
                    'regex
                    pat1 = ".*span.*>(.+?)<"
                    Date1 = simpleRegex(sHtml, pat1, 0)
                    Date2 = simpleRegex(sHtml, pat1, 1)
                        MsgBox Date1 & " " & Date2
    
    End Sub
    

    正则表达式函数

    Function simpleRegex(strInput As String, strPattern As String, sNr As Long)
        Dim regEx As New RegExp
        If strPattern <> "" Then
            With regEx
                .Global = True
                .MultiLine = True
                .IgnoreCase = True
                .Pattern = strPattern
            End With
            dfs = regEx.Test(strInput)
            If regEx.Test(strInput) Then
                Set sReg = regEx.Execute(strInput)
                simpleRegex = sReg(sNr).SubMatches(0)
            Else
                simpleRegex = "false"
            End If
        End If
    End Function
    

    【讨论】:

      【解决方案4】:

      您可以通过使用 github 上的 HTML DOM 库来实现这一点,但我使用 sourceforge 在此链接上下载 https://simplehtmldom.sourceforge.io

      如下使用

      // Create DOM from URL or file
      $html = file_get_html('http://www.google.com/');
      
      // Find all images 
      foreach($html->find('img') as $element) 
      echo $element->src . '<br>';
      
      // Find all links 
      foreach($html->find('a') as $element) 
      echo $element->href . '<br>';
      

      你应该使用 span 作为

      // find('span.data-email-preview')  if not work use  find('date-email-preview')
      

      如果你想使用 preg_replace 它很容易,但会令人困惑,因为有很多值,所以输出会有很多日期,然后你必须制作这个输出的数组,然后进行循环以在单行中查看每个日期,这样你可以导入数据库

      【讨论】:

        猜你喜欢
        • 2021-11-22
        • 1970-01-01
        • 2015-02-26
        • 1970-01-01
        • 1970-01-01
        • 2021-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多