2014-02-26 57 views
0

我想知道爲什麼MSXML2.ServerXMLHTTP對象的響應屬性沒有返回完整的html源代碼。它似乎只是返回「內部html」。我可以創建一個IE對象並獲取「外部html」,但由於我擁有數百個搜索項,因此效率不高。爲什麼不是MSXML2.ServerXMLHTTP響應返回完整的html源代碼?

我有下面顯示的功能(與URL),將HTML內容分配給一個字符串。

Sub test() 
    Dim myString As String 
    myString = getECICS2("103-90-2") ' myString only contains inner html 
End Sub 

Public Function getECICS(ByVal casNum As String) As String 
    Dim XMLhttp: Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP") 
    XMLhttp.setTimeouts 2000, 2000, 2000, 2000 
    XMLhttp.Open "GET", "http://ec.europa.eu/taxation_customs/dds2/ecics/chemicalsubstance_consultation.jsp?Lang=en&Cas=" & casNum & "&Cus=&CnCode=&EcCode=&UnCode=&Name=&LangNm=en&Inchi=&Characteristic=&sortOrder=1&Expand=true&offset=0&range=25", False 
    XMLhttp.send 
    If XMLhttp.Status = 200 Then 
    getECICS = XMLhttp.responseText 
    Else 
    getECICS = "" 
    End If 
End Function 

在此先感謝

+0

無論我使用您的方法還是僅僅在瀏覽器中查看源代碼,我都會得到完全相同的結果。你應該看看源代碼 - 在開頭的''標籤前面有一堆腳本。 –

+0

是的我知道但是我對此不感興趣,我感興趣的部分是在頁面的底部,例如對於這個特定的搜索它是「0021314-9」,但是這個數字不會使用我的方法出現。有趣的是,如果我去firefox督察> HTML>複製外部HTML,剪貼板包含搜索結果 – Jeanno

+0

你如何測試該「0021314-9」的存在?如果你使用debug.print,你應該知道它只顯示最多行數。否則 - 請擴大。 –

回答

1

添擊中了要害。一旦下載了html,網頁就使用javascript來更新頁面。這在瀏覽器中自動發生。

如果您運行下面的代碼將傾倒的響應轉換成HTML文件,你可以在Chrome/IE瀏覽/ FF等

Sub test() 
    Dim myString As String 
    myString = getECICS("103-90-2") ' myString only contains inner html 
End Sub 

Public Function getECICS(ByVal casNum As String) As String 
    Dim XMLhttp: Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP") 
    XMLhttp.setTimeouts 2000, 2000, 2000, 2000 
    XMLhttp.Open "GET", "http://ec.europa.eu/taxation_customs/dds2/ecics/chemicalsubstance_consultation.jsp?Lang=en&Cas=" & casNum & "&Cus=&CnCode=&EcCode=&UnCode=&Name=&LangNm=en&Inchi=&Characteristic=&sortOrder=1&Expand=true&offset=0&range=25", False 
    XMLhttp.send 
    If XMLhttp.Status = 200 Then 
    getECICS = XMLhttp.responseText 
    Else 
    getECICS = "" 
    End If 
    outputtext (getECICS) 
End Function 

Function outputtext(text As String) 
Dim MyFile As String, fnum As String 
     MyFile = ThisWorkbook.Path & "\" & "test.html" 
     'set and open file for output 
     fnum = FreeFile() 
     Open MyFile For Output As fnum 
     'use Print when you want the string without quotation marks 
     Print #fnum, text 
     Close #fnum 
End Function 

不幸的是,最簡單的解決方法是在瀏覽器中運行的自動化或腳本啓用的解決方案來獲取所需的數據。

現在很多網站都使用javascript/AJAX/Login會話來控制近期的資源訪問速度和速度,所以您不能總是通過不使用瀏覽器來獲得期望的速度。

0

有看的XMLHttpRequest其他方法...

responseText返回響應正文文本

responseXML返回身體作爲一個DOM對象

我想你是:XMLhttp.response它返回整個響應。

或者:XMLhttp.responseBody

我不是完全肯定在這個'因爲我只使用C++接口我自己。

看到:http://msdn.microsoft.com/en-us/library/windows/apps/hh453379.aspx#methods

+0

thannks你的迴應,但我恐怕使用XMLhttp.response沒有改變任何東西。我在字符串中獲得相同的內容。 – Jeanno

+0

vba中提供了'responseBody'屬性嗎?請嘗試一下... – TonyWilk

+0

它確實如此,但它回覆了一些奇怪的東西,我認爲Tim Williams可能有答案,您不能在動態頁面中使用XMLhttp。 – Jeanno

相關問題