【问题标题】:Copying Text String From Website Into Excel VBA将文本字符串从网站复制到 Excel VBA
【发布时间】:2020-10-09 12:28:44
【问题描述】:

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

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

Lorem ipsum dolor sit amet,consectetur adipiscing elit。

代码需要将“sectetur adipiscing elit”值返回到指定单元格。即字符串本身,以及字符串左侧的 20 个字符。

这是我到目前为止的想法(我知道 .select 不是最佳做法,但它对我有用):

Sub String_Checker()
Sheets("Sheet1").Range("a2").Select
Dim IE As Object
Do Until IsEmpty(ActiveCell)
    Set IE = CreateObject("internetexplorer.Application")
    IE.Visible = True
    IE.navigate "https://website.com"
     Do Until (IE.readyState = 4 And Not IE.Busy)
    
          DoEvents
Loop
Set objDoc = IE.document
strMyPage = objDoc.body.innerHTML
Dim s As String: s = ActiveCell.Offset(0, 1).Value
ActiveCell.Offset(0, 2).Value = Left(strMyPage, 20)

IE.Quit
ActiveCell.Offset(1, 0).Select
Loop
End Sub

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

【问题讨论】:

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

标签: html excel vba


【解决方案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

【讨论】:

  • 嘿,有效的糟糕解决方案总比无效的好解决方案好!这通过一些小的调整就成功了。我在循环指令上方设置了工作簿定义,它运行起来就像一个魅力。感谢您和所有为您的见解发表评论的人!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
  • 2012-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多