gpt4 book ai didi

vba - 获取html源代码到Excel多行

转载 作者:行者123 更新时间:2023-12-04 20:43:41 26 4
gpt4 key购买 nike

我对 Excel VBA 非常陌生,我需要抓取网站的 HTML。
现在这对我来说是一项艰巨的任务,我有一个解决方法 - 使用 VBA 将整个 HTML 源代码获取到工作表,然后使用 Excel 函数进行抓取。

这是来自其他站点的 VBA 代码,它们将整个 HTML 源代码获取到工作表中:

Sub ExtractWeb()

'to refer to the running copy of Internet Explorer
Dim ie As InternetExplorer

'to refer to the HTML document returned
Dim html As HTMLDocument

'open Internet Explorer in memory, and go to website
Set ie = New InternetExplorer
ie.Visible = False
ie.navigate "http://www.google.com"

'Wait until IE is done loading page
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop

'show text of HTML document returned
Set html = ie.document

Worksheets("test").Select
Range("A1").Value = html.DocumentElement.outerHTML

End Sub

问题是:包含许多行的整个源代码被粘贴到一个单元格中。
但是我希望源代码的每一行都插入一行。
我应该如何更正我的代码?

最佳答案

Dim arr

arr = Split(html.DocumentElement.outerHTML, vbLf) 'or vbCR or vbCrLf

Worksheets("test").Range("A1").Resize( UBound(arr)+1, 1 ).Value = arr

关于vba - 获取html源代码到Excel多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25791378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com