Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何用VBA解析网站标题_Excel_Vba - Fatal编程技术网

Excel 如何用VBA解析网站标题

Excel 如何用VBA解析网站标题,excel,vba,Excel,Vba,我正在尝试接收标题,但我的功能不起作用。 GetHTTPResponse运行良好,我收到HTML代码 但是GetTitle不会输出任何数据 Function GetHTTPResponse(ByVal sURL As String) As String On Error Resume Next Set oXMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") With oXMLHTTP

我正在尝试接收标题,但我的功能不起作用。
GetHTTPResponse
运行良好,我收到HTML代码 但是
GetTitle
不会输出任何数据

Function GetHTTPResponse(ByVal sURL As String) As String
    On Error Resume Next
    Set oXMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    With oXMLHTTP
        .Open "GET", sURL, False
        .send
        GetHTTPResponse = .responseText
    End With
    Set oXMLHTTP = Nothing
End Function

Function GetTitle()
Data = GetHTTPResponse("https://www.google.com/")
findTag = "<title>"
Search = InStr(1, Data, findTag)
End Function 
函数GetHTTPResponse(ByVal sURL作为字符串)作为字符串
出错时继续下一步
设置oXMLHTTP=CreateObject(“WinHttp.WinHttpRequest.5.1”)
使用oXMLHTTP
.打开“GET”,sURL,False
.发送
GetHTTPResponse=.responseText
以
设置oXMLHTTP=Nothing
端函数
函数GetTitle()
数据=GetHTTPResponse(“https://www.google.com/")
findTag=“”
Search=InStr(1,数据,findTag)
端函数
我在窗口里跑
?GetTitle()


如何解决它?

GetTitle
不返回任何值(缺少
GetTitle=…

您可以将
responseText
作为
innerHTML
放在
HTMLfile
对象中,并使用DOM(文档对象模型)。使用DOM方法
getElementsByTagName()
可以获得比的标题。

(1) 将选项显式放置在模块顶部(2)声明(Dim)所有变量。(3) 摆脱“错误时继续下一步”。(4) 逐步完成代码并检查每个变量的值。这应该让你开始。
Sub GetHTTPResponse()
  
  Const url As String = "https://www.google.com/"
  Dim doc As Object
  
  Set doc = CreateObject("htmlfile")
  
  With CreateObject("MSXML2.XMLHTTP.6.0")
    .Open "GET", url, False
    .send
    If .Status = 200 Then
      doc.body.innerHTML = .responseText
      MsgBox doc.getElementsByTagName("title")(0).innerText
    Else
      MsgBox "Page not loaded. HTTP status: " & .Status
    End If
  End With
End Sub