Html 将文本字符串从网站复制到Excel VBA

Html 将文本字符串从网站复制到Excel VBA,html,excel,vba,Html,Excel,Vba,我需要让VBA查看网站的HTML,在文本中找到某个字符串,然后在Excel单元格中放置一个包含该字符串的值,并在该字符串的左侧添加X个字符,例如20个字符 例如,如果我需要在包含以下字符串的站点中查找字符串“elit”: Lorem ipsum dolor sit amet,是一位杰出的献身者 代码需要向指定的单元格返回值“sectetur adipising elit”。也就是说,字符串本身,以及字符串左侧的20个字符 以下是我到目前为止的想法(我知道,select不是最佳实践,但它对我很有效

我需要让VBA查看网站的HTML,在文本中找到某个字符串,然后在Excel单元格中放置一个包含该字符串的值,并在该字符串的左侧添加X个字符,例如20个字符

例如,如果我需要在包含以下字符串的站点中查找字符串“elit”:

Lorem ipsum dolor sit amet,是一位杰出的献身者

代码需要向指定的单元格返回值“sectetur adipising elit”。也就是说,字符串本身,以及字符串左侧的20个字符

以下是我到目前为止的想法(我知道,select不是最佳实践,但它对我很有效):

这给了我HTML的最后20个字符,但我需要让代码开始“查看”指定的字符串,该字符串在Excel中始终是ActiveCell.Offset(0,1).Value。任何帮助都将不胜感激。谢谢

来自innerHTML的字符串
  • 自上而下这是一个糟糕的解决方案,但我的调查导致了它,希望它能起到作用
代码

Option Explicit

Sub String_Checker()

' I only ran this from VBE. Sometimes the following error would occur:
' Run-time error '2125463506 (8150002e)':
' The text associated with this error code could not be found.
' I don't know why.
    
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    'IE.Visible = True
    IE.navigate "https://www.wikipedia.com"
    Do Until (IE.readyState = 4 And Not IE.Busy)
        DoEvents
    Loop
    Dim objdoc As Object
    Set objdoc = IE.document
    Dim strMyPage As String
    strMyPage = objdoc.body.innerHTML
    IE.Quit
    
    Const pLeft As Long = 20
    
    Dim wb As Workbook
    Set wb = ThisWorkbook ' The workbook containing this code.
    Dim ws As Worksheet
    Set ws = wb.Worksheets("Sheet1")
    
    Dim cel As Range
    Set cel = ws.Range("A2")
    Dim s As String
    Dim pStart As Long
    Dim pLen As Long
    
    Do Until IsEmpty(cel)
        s = cel.Offset(0, 1).Value
        pStart = InStr(1, strMyPage, s, vbTextCompare) - pLeft
        If pStart > 0 Then
        ' The string ('s') was found.
            pLen = InStr(1, strMyPage, s, vbTextCompare) + Len(s) - pStart
            s = Mid(strMyPage, pStart, pLen)
            On Error Resume Next
            ' Here I would receive the following error:
            ' Run-time error '1004': Application-defined or object-defined error
            ' It would occur when the first character would be "=".
            cel.Offset(0, 2).Value = s
            If Err.Number <> 0 Then
                cel.Offset(0, 2).Value = "'" & s ' Maybe this can always be used.
            End If
            On Error GoTo 0
        Else
        ' The string ('s') was NOT found.
        End If
        Set cel = cel.Offset(1)
    Loop

End Sub
选项显式
子字符串检查程序()
“我只从VBE运行了这个。有时会出现以下错误:
'运行时错误'2125463506(8150002e)':
'找不到与此错误代码关联的文本。
“我不知道为什么。
模糊的物体
设置IE=CreateObject(“InternetExplorer.Application”)
'IE.Visible=True
即“导航”https://www.wikipedia.com"
直到(IE.readyState=4且不IE.Busy)
多芬特
环
作为对象的Dim objdoc
设置objdoc=IE.document
Dim strMyPage作为字符串
strMyPage=objdoc.body.innerHTML
即退出
长度=20时的常数
将wb设置为工作簿
将wb=ThisWorkbook设置为包含此代码的工作簿。
将ws设置为工作表
设置ws=wb.工作表(“表1”)
暗淡的cel As范围
设置cel=ws.范围(“A2”)
像线一样变暗
暗淡的pStart尽可能长
长得一样暗
直到我空为止(cel)
s=单元偏移量(0,1).值
pStart=InStr(1,strMyPage,s,vbTextCompare)-pLeft
如果pStart>0,则
'找到了字符串('s')。
pLen=仪表(1,标准页,s,VBTEXT比较)+透镜(s)-pStart
s=Mid(标准页、pStart、pLen)
出错时继续下一步
'在这里,我将收到以下错误:
“运行时错误”1004:应用程序定义的错误或对象定义的错误
'当第一个字符为“=”时,将出现此错误。
单元偏移量(0,2)。值=s
如果错误号为0,则
cel.Offset(0,2).Value=“”“&s”可能始终可以使用此选项。
如果结束
错误转到0
其他的
'未找到字符串('s')。
如果结束
设置单元=单元偏移(1)
环
端接头
来自innerHTML的字符串
  • 自上而下这是一个糟糕的解决方案,但我的调查导致了它,希望它能起到作用
代码

Option Explicit

Sub String_Checker()

' I only ran this from VBE. Sometimes the following error would occur:
' Run-time error '2125463506 (8150002e)':
' The text associated with this error code could not be found.
' I don't know why.
    
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    'IE.Visible = True
    IE.navigate "https://www.wikipedia.com"
    Do Until (IE.readyState = 4 And Not IE.Busy)
        DoEvents
    Loop
    Dim objdoc As Object
    Set objdoc = IE.document
    Dim strMyPage As String
    strMyPage = objdoc.body.innerHTML
    IE.Quit
    
    Const pLeft As Long = 20
    
    Dim wb As Workbook
    Set wb = ThisWorkbook ' The workbook containing this code.
    Dim ws As Worksheet
    Set ws = wb.Worksheets("Sheet1")
    
    Dim cel As Range
    Set cel = ws.Range("A2")
    Dim s As String
    Dim pStart As Long
    Dim pLen As Long
    
    Do Until IsEmpty(cel)
        s = cel.Offset(0, 1).Value
        pStart = InStr(1, strMyPage, s, vbTextCompare) - pLeft
        If pStart > 0 Then
        ' The string ('s') was found.
            pLen = InStr(1, strMyPage, s, vbTextCompare) + Len(s) - pStart
            s = Mid(strMyPage, pStart, pLen)
            On Error Resume Next
            ' Here I would receive the following error:
            ' Run-time error '1004': Application-defined or object-defined error
            ' It would occur when the first character would be "=".
            cel.Offset(0, 2).Value = s
            If Err.Number <> 0 Then
                cel.Offset(0, 2).Value = "'" & s ' Maybe this can always be used.
            End If
            On Error GoTo 0
        Else
        ' The string ('s') was NOT found.
        End If
        Set cel = cel.Offset(1)
    Loop

End Sub
选项显式
子字符串检查程序()
“我只从VBE运行了这个。有时会出现以下错误:
'运行时错误'2125463506(8150002e)':
'找不到与此错误代码关联的文本。
“我不知道为什么。
模糊的物体
设置IE=CreateObject(“InternetExplorer.Application”)
'IE.Visible=True
即“导航”https://www.wikipedia.com"
直到(IE.readyState=4且不IE.Busy)
多芬特
环
作为对象的Dim objdoc
设置objdoc=IE.document
Dim strMyPage作为字符串
strMyPage=objdoc.body.innerHTML
即退出
长度=20时的常数
将wb设置为工作簿
将wb=ThisWorkbook设置为包含此代码的工作簿。
将ws设置为工作表
设置ws=wb.工作表(“表1”)
暗淡的cel As范围
设置cel=ws.范围(“A2”)
像线一样变暗
暗淡的pStart尽可能长
长得一样暗
直到我空为止(cel)
s=单元偏移量(0,1).值
pStart=InStr(1,strMyPage,s,vbTextCompare)-pLeft
如果pStart>0,则
'找到了字符串('s')。
pLen=仪表(1,标准页,s,VBTEXT比较)+透镜(s)-pStart
s=Mid(标准页、pStart、pLen)
出错时继续下一步
'在这里,我将收到以下错误:
“运行时错误”1004:应用程序定义的错误或对象定义的错误
'当第一个字符为“=”时,将出现此错误。
单元偏移量(0,2)。值=s
如果错误号为0,则
cel.Offset(0,2).Value=“”“&s”可能始终可以使用此选项。
如果结束
错误转到0
其他的
'未找到字符串('s')。
如果结束
设置单元=单元偏移(1)
环
端接头

找到字符串后,您希望在单元格中返回什么?找到的字符串的左边部分还是右边部分?我认为您应该使用
innertext
而不是
innerhtml
@vbasic208:找到的字符串的左边部分以及字符串本身。是否有一个实际的url与此匹配?您使用的是字符串函数而不是html解析器,因为您使用的是不同的网页吗?@QHarr:很遗憾,我无法提供具体的网站,因为它是保密的。虽然我将要从中提取数据的网站每次都是相同的,但URL末尾有一个通配符,它会随着每次使用而改变。我已经在代码中说明了这个通配符,因此脚本将导航到正确的网页。如果HTML解析器对您更有意义(听起来确实像我应该使用的),我希望您能提供更多关于如何使用的信息。当找到字符串时,您希望在单元格中返回什么?找到的字符串的左边部分还是右边部分?我认为应该使用
innertext
而不是