使用vba打印/导入所有网页源数据

时间:2017-02-20 05:00:28

标签: excel vba excel-vba dom

我下面的代码只将部分源代码导入到工作表中。我想要所有源代码,因为它是.Sub GetSourceCode()

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`

2 个答案:

答案 0 :(得分: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

希望这对你有用!

答案 1 :(得分: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

更改要保存的位置。