【问题标题】:Unable to scoop out specific portions from a webpage using regex无法使用正则表达式从网页中挖出特定部分
【发布时间】:2019-12-13 02:21:27
【问题描述】:

以下用 vba 编写的脚本可以使用 xhr 从 webpage 中解析出一些 json 内容中的名称。我知道那里也有一个 vba json 转换器来解析来自 json 内容的信息。如果我知道在这种情况下应用正则表达式的方法,我就可以创建模式来解决问题。

当前尝试(工作中的一个):

Sub GetNames()
    Dim str As Variant, N&, R&, rxp As New RegExp

    With New XMLHTTP60
        .Open "GET", "https://oresapp.asicanada.net/ores.imis.services/api/member/?address=&callback=angular.callbacks._0&city=&companyName=&personName=", False
        .send
        str = Split(.responseText, ":[{""Id"":")
    End With

    N = UBound(str)

    For R = 1 To N
        Cells(R, 1) = Split(Split(str(R), "FullName"":""")(1), """")(0)
    Next R
End Sub

难道不能使用正则表达式从上述链接中解析名称吗?

【问题讨论】:

    标签: regex excel vba web-scraping


    【解决方案1】:

    是的。您可以按如下方式使用惰性正则表达式

    Option Explicit
    
    Public Sub GetFullNames()
        Dim results(), matches As Object, s As String
        With CreateObject("MSXML2.XMLHTTP")
            .Open "GET", "https://oresapp.asicanada.net/ores.imis.services/api/member/?address=&callback=angular.callbacks._0&city=&companyName=&personName=", False
            .send
            s = .responsetext
        End With
        With CreateObject("VBScript.RegExp")
            .Global = True
            .IgnoreCase = False
            .Pattern = "FullName"":""(.*?)"""
            .MultiLine = True
            Set matches = .Execute(s)
            ReDim results(1 To matches.Count)
       End With
       Dim match As Variant, r As Long
       For Each match In matches
           r = r + 1
           results(r) = match.submatches(0)
       Next
       With ThisWorkbook.Worksheets("Sheet1")
           .Cells(1, 1).Resize(UBound(results), 1) = Application.Transpose(results)
       End With
    End Sub
    


    Lazy quantifier:

    懒惰的 .*?保证量化的点只匹配尽可能多的 模式的其余部分成功所需的字符。 因此,该模式一次只匹配一个 {START}...{END} 项, 这就是我们想要的。


    没有数组:

    Option Explicit
    Public Sub GetFullNames()
        Dim matches As Object, s As String
        With CreateObject("MSXML2.XMLHTTP")
            .Open "GET", "https://oresapp.asicanada.net/ores.imis.services/api/member/?address=&callback=angular.callbacks._0&city=&companyName=&personName=", False
            .send
            s = .responsetext
        End With
        With CreateObject("VBScript.RegExp")
            .Global = True
            .IgnoreCase = False
            .Pattern = "FullName"":""(.*?)"""
            .MultiLine = True
            Set matches = .Execute(s)
        End With
        Dim match As Variant, r As Long
        For Each match In matches
            r = r + 1
            With ThisWorkbook.Worksheets("Sheet1")
                .Cells(r, 1) = match.submatches(0)
            End With
        Next
    End Sub
    

    【讨论】:

    • 它出色地完成了这项工作。对此提供一点帮助将不胜感激。如果不是 Array,我将如何做同样的事情?非常感谢。
    • 您的意思是不将结果存储在数组中?
    • 没错。 ReDim 仍在您的编辑中。
    • 哎呀。对不起!已移除
    • 我希望编辑中也有一个按钮可以投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2021-12-28
    • 2014-12-31
    • 2016-12-27
    • 2015-01-05
    • 2023-03-23
    相关资源
    最近更新 更多