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

我有下面的代码,只input部分源代码到工作表。 我想要所有的源代码.`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`

嗨,你可以参考下面的代码

 ' 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

希望这对你有用!

你可以把源代码保存在这样的文本文件中。 添加下面的函数而不是这行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 

更改您要保存的位置。