2017-02-20 60 views
0

我有下面的代碼將部分源代碼導入工作表。我想所有的源代碼,因爲它is.`Sub GetSourceCode()使用vba打印/導入所有的網頁源數據

Dim ie As Object 

Dim str As String 
Dim arr 

str = Sheets("sheet2").Range("I1").Value 
Set ie = CreateObject("INTERNETEXPLORER.APPLICATION") 

ie.Navigate "https://tiweb.industrysoftware.automation.com/prdata/cgi-bin/n_prdata_index.cgi?" 
ie.Visible = False 

Do Until ie.ReadyState = 4 
    DoEvents 
Loop 

ie.Document.getelementsbyname("pr_numbers")(0).Value = str 
Application.SendKeys ("~") 

Do Until ie.ReadyState = 4 
    DoEvents 
Loop 


Worksheets("Download_PRdata2").Activate 

arr = Split(ie.Document.body.outertext) 
Worksheets("Download_PRdata2").Activate 
ActiveSheet.Range("A1").Resize(UBound(arr) + 1, 1).Value = Application.Transpose(arr) 

末Sub`

回答

1

嗨,你可以參考下面的代碼

' Fetch Entire Source Code 

Private Sub HTML_VBA_Excel() 
Dim oXMLHTTP As Object 
Dim sPageHTML As String 
Dim sURL As String 

'Change the URL before executing the code 
sURL = "http://www.google.com" 

'Extract data from website to Excel using VBA 
Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP") 
oXMLHTTP.Open "GET", sURL, False 
oXMLHTTP.send 
sPageHTML = oXMLHTTP.responseText 

'Get webpage data into Excel 
' If longer sourcecode mean, you need to save to a external text file or somewhere, 
' since excel cell have some limits on storing max characters 

ThisWorkbook.Sheets(1).Cells(1, 1) = sPageHTML 

MsgBox "XMLHTML Fetch Completed" 

End Sub 

來源:http://www.vbausefulcodes.in/usefulcodes/get-data-or-source-code-from-webpage-using-excel-vba.php

希望這對你有用!

+0

我的網頁可能有很大的信息,無法保存到單元格中。另外我想插入數據到網頁,然後導入數據。現在我手動將源代碼保存到某個位置,然後用宏導入它。我想要宏以某種方式導入整個數據,將pr_numbers從單元格插入網頁並導入顯示頁面的源代碼。 –

1

您可以將源代碼保存在這樣的文本文件中。添加以下功能而不是此線ThisWorkbook.Sheets(1).Cells的(1,1)= sPageHTML

Createtextfile (sPageHTML) 

和End Sub後添加此功能下面。

Sub Createtextfile(sPageHTML) 

Dim fso As Object 
Set fso = CreateObject("Scripting.FileSystemObject") 
Dim oFile As Object 
strPath = "E:\test.txt" 
Set oFile = fso.Createtextfile(strPath) 
oFile.WriteLine sPageHTML 
oFile.Close 
Set fso = Nothing 
Set oFile = Nothing 

End Sub 

更改要保存的位置。

+0

不錯,它幾乎是我想要的,但是用原始代碼(我的)試過,因爲我想從單元格插入文本到網頁,然後繼續並保存該頁面的源代碼。你能在這方面幫助我嗎? –

+0

有沒有人看這個? –