2016-11-03 2 views
1

웹 페이지에서 콘텐츠를 추출하고 싶습니다. 그러나 응답 텍스트를 받으면 자바 스크립트가 포함되어 브라우저에서 열어 본 페이지처럼 처리 할 수 ​​없습니다.VBA - XMLHTTP GET 요청에서 JavaScript 콘텐츠 다루기

이 방법을 사용하여 HTML 콘텐츠를 가져 오거나 브라우저 에뮬레이션 만 도움이 될 수 있습니까? 아니면이 콘텐츠를 수신하는 몇 가지 다른 방법이 있습니까?

Dim oXMLHTTP As New MSXML2.XMLHTTP 
Dim htmlObj As New HTMLDocument 

With oXMLHTTP 
    .Open "GET", "http://www.manta.com/ic/mtqyfk0/ca/riverbend-holdings-inc", False 
    .send 

    If .ReadyState = 4 And .Status = 200 Then    
     htmlObj.body.innerHTML = .responseText 
     'do things 
    End If 

End With 

응답 텍스트 :

<!DOCTYPE html> 
<head> 
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"> 
<meta http-equiv="cache-control" content="max-age=0" /> 
<meta http-equiv="cache-control" content="no-cache" /> 
<meta http-equiv="expires" content="0" /> 
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> 
<meta http-equiv="pragma" content="no-cache" /> 
<meta http-equiv="refresh" content="10; url=/distil_r_blocked.html?Ref=/ic/mtq599v/ca/45th-street-limited-partnership&amp;distil_RID=2115B138-A1BF-11E6-A957-C0595F6B962F&amp;distil_TID=20161103121454" /> 
<script type="text/javascript"> 
    (function(window){ 
     try { 
      if (typeof sessionStorage !== 'undefined'){ 
       sessionStorage.setItem('distil_referrer', document.referrer); 
      } 
     } catch (e){} 
    })(window); 
</script> 
<script type="text/javascript" src="/ser-yrbwqfedrrwwvctvyavy.js" defer></script><style type="text/css">#d__fFH{position:absolute;top:-5000px;left:-5000px}#d__fF{font-family:serif;font-size:200px;visibility:hidden}#verxvaxcuczwcwecuxsx{display:none!important}</style></head> 
<body> 
<div id="distil_ident_block">&nbsp;</div> 
</body> 
</html> 
+1

[일반적으로] 서버가 응답을 보내면받는 메시지가 전송됩니다. "HTML 만"을 요청할 수는 없습니다 (서버가 어떤 식 으로든 이것을 지원하도록 구성되어 있지 않은 경우는 거의 없습니다). 동적 인 내용을 다룰 수있는 유일한 방법은 브라우저 자동화/셀레늄/등입니다. –

+1

스크립트를 가져 오는 이유는 HTML 파일에 직접 있기 때문입니다. HTML 파서를 사용하여 컨텐트를 다운로드 한 후 스크립트 태그를 제거 할 수 있습니다. DOM을 구문 분석하는 방법에 대한 다음 스레드를 참조 할 수 있습니다 (http://stackoverflow.com/a/28917205/1640090). – vbguyny

답변

0

아니오 - 자바 스크립트가 실제로 <script> 태그 내부의 HTML의 일부이기 때문이다. 태그를 직접 제거하려면 응답을 사후 처리해야합니다. 이것은과 같이 샘플 코드에 포함 할 수

Function RemoveScriptTags(objHTML As HTMLDocument) As String 

    Dim objElement As HTMLGenericElement 

    For Each objElement In objHTML.all 
     If VBA.LCase$(objElement.nodeName) = "script" Then 
      objElement.removeNode 
     End If 
    Next objElement 

    RemoveScriptTags = objHTML.DocumentElement.outerHTML 

End Function 

:

Option Explicit 

Sub Test() 

    Dim objXMLHTTP As New MSXML2.XMLHTTP 
    Dim objHTML As Object 
    Dim strUrl As String 
    Dim strHtmlNoScriptTags As String 

    strUrl = "http://www.manta.com/ic/mtqyfk0/ca/riverbend-holdings-inc" 

    With objXMLHTTP 
     .Open "GET", strUrl, False 
     .send 

     If .ReadyState = 4 And .Status = 200 Then 
      Set objHTML = CreateObject("htmlfile") 
      objHTML.Open 
      objHTML.write objXMLHTTP.responseText 
      objHTML.Close 

      'do things 
      strHtmlNoScriptTags = RemoveScriptTags(objHTML) 
      Debug.Print strHtmlNoScriptTags 

      'update html document with script-less document 
      Set objHTML = CreateObject("htmlfile") 
      objHTML.Open 
      objHTML.write strHtmlNoScriptTags 
      objHTML.Close 

      'you can now operate on DOM of objHTML 

     End If 

    End With 

End Sub 
당신은 페이지의 HTML을받은 후

당신은 DOM에서 <script> 노드를 제거하는 기능을 사용할 수 있습니다

+0

이 함수를 사용한 후에 표시되는 응답 텍스트는

 
입니다. 그것은 JS 태그를 제거했습니다 - 그렇습니다. 그러나 어떤 방법으로도 broswer를 사용하면이 스크립트에 의해 생성 된 HTML 문서에서 작동시키지 않습니다. –

+0

제 편집을 확인하십시오 - 전체 문서 (적은 스크립트) 태그가되도록 함수의 반환 값을 수정하고이 텍스트를 HTMLDocument에 다시 넣어 DOM 객체로 작업 할 수있는 방법을 보여줍니다. –

관련 문제