Html vba中的Webscraping-结构工作数据&;从左向右写入单元格

Html vba中的Webscraping-结构工作数据&;从左向右写入单元格,html,excel,vba,web-scraping,Html,Excel,Vba,Web Scraping,刚刚在这里注册了一个账户,是的,我是一个真正的noob-请对我好一点。现在我面临的挑战是:我正在用VBA构建一个web刮板&我发现了一个代码,我根据自己的需要做了一些修改。一切都很完美,实际上相当顺利。现在,我希望加载到exel文档中的文本不要太长,而是要很宽。我怀疑这与“.Offset(I,j)”有关。我已经玩了一点,但我只是设法破坏了一切。这是我使用的代码: Dim IE As InternetExplorer Dim htmldoc As MSHTML.IHTMLDocument 'Doc

刚刚在这里注册了一个账户,是的,我是一个真正的noob-请对我好一点。现在我面临的挑战是:我正在用VBA构建一个web刮板&我发现了一个代码,我根据自己的需要做了一些修改。一切都很完美,实际上相当顺利。现在,我希望加载到exel文档中的文本不要太长,而是要很宽。我怀疑这与“.Offset(I,j)”有关。我已经玩了一点,但我只是设法破坏了一切。这是我使用的代码:

Dim IE As InternetExplorer
Dim htmldoc As MSHTML.IHTMLDocument 'Document object
Dim eleColtr As MSHTML.IHTMLElementCollection 'Element collection for tr tags
Dim eleColtd As MSHTML.IHTMLElementCollection 'Element collection for td tags
Dim eleRow As MSHTML.IHTMLElement 'Row elements
Dim eleCol As MSHTML.IHTMLElement 'Column elements
Dim ieURL As String 'URL

'Open InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
'Navigate to webpage
ieURL = "#"
IE.Navigate ieURL
'Wait
Do While IE.Busy Or IE.ReadyState <> 4
 DoEvents
Loop
Set htmldoc = IE.Document 'Document webpage
Set eleColtr = htmldoc.getElementsByTagName("tr") 'Find all tr tags
'This section populates Excel
I = 0 'start with first value in tr collection
For Each eleRow In eleColtr 'for each element in the tr collection
 Set eleColtd = htmldoc.getElementsByTagName("tr")(I).getElementsByTagName("td") 'get all the td elements in that specific tr
 j = 0 'start with the first value in the td collection
 For Each eleCol In eleColtd 'for each element in the td collection
 Sheets("Sheet1").Range("A1").Offset(I, j).Value = eleCol.innerText 'paste the inner text of the td element, and offset at the same time
 j = j + 1 'move to next element in td collection
 Next eleCol 'rinse and repeat
 I = I + 1 'move to next element in td collection
Next eleRow 'rinse and repeat

End Sub ```
Dim IE作为InternetExplorer
将htmldoc设置为MSHTML.IHTMLDocument的文档对象
Dim eleColtr作为tr标记的MSHTML.IHTMLElementCollection元素集合
Dim eleColtd作为td标记的MSHTML.IHTMLElementCollection元素集合
Dim eleRow作为MSHTML.IHTMLElement的行元素
Dim eleCol作为MSHTML.IHTMLElement的列元素
Dim ieURL作为字符串的URL
“开放式InternetExplorer
设置IE=CreateObject(“InternetExplorer.Application”)
可见=真实
'导航到网页
ieURL=“#”
浏览ieURL
“等等
在忙或准备状态4时执行
多芬特
环
设置htmldoc=IE.Document的文档网页
Set eleColtr=htmldoc.getElementsByTagName(“tr”)'查找所有tr标记
'此部分填充Excel
I=0'从tr集合中的第一个值开始
对于eleColtr中的每个eleRow'对于tr集合中的每个元素
Set eleColtd=htmldoc.getElementsByTagName(“tr”)(I).getElementsByTagName(“td”)获取该特定tr中的所有td元素
j=0'从td集合中的第一个值开始
对于eleColtd中的每个eleCol,对于td集合中的每个元素
图纸(“Sheet1”).范围(“A1”).偏移量(I,j).Value=eleCol.innerText'粘贴td元素的内部文本,同时偏移
j=j+1'移动到td集合中的下一个元素
下一步,eleCol'冲洗并重复
I=I+1'移动到td集合中的下一个元素
下一步,冲洗并重复
端接头```

您不需要浏览器。您可以使用更快的xhr。抓取表并循环行,然后循环列填充预先调整大小的数组(确保删除标题所在的行。它们可以被标识为在第一个
td
中有
[colspan='2']]
)。然后转置数组并写入工作表

Option Explicit

Public Sub TransposeTable()
    Dim xhr As MSXML2.XMLHTTP60, html As MSHTML.HTMLDocument, table As MSHTML.htmltable
    'required VBE (Alt+F11) > Tools > References > Microsoft HTML Object Library ;  Microsoft XML, v6 (your version may vary)

    Set xhr = New MSXML2.XMLHTTP60
    Set html = New MSHTML.HTMLDocument
    '  7NXBG2 ;  8QT2E3

    With xhr
        .Open "GET", "https://www.chrono24.com/watch/8QT2E3", False
        .send
        html.body.innerHTML = .responseText
    End With

    Set table = html.querySelector(".specifications table")

    Dim results(), rowCountToExclude As Long

    rowCountToExclude = html.querySelectorAll(".specifications table [colspan='2']").Length
    ReDim results(1 To table.rows.Length - rowCountToExclude, 1 To table.getElementsByTagName("tr")(0).Children(0).getAttribute("colspan"))

    Dim r As Long, c As Long, outputRow As Long, outputColumn As Long, html2 As MSHTML.HTMLDocument

    Set html2 = New MSHTML.HTMLDocument

    For r = 0 To table.getElementsByTagName("tr").Length - 1
        Dim row As Object

        Set row = table.getElementsByTagName("tr")(r)
        html2.body.innerHTML = "<body> <table>" & row.outerHTML & "</table></body> "

        If html2.querySelectorAll("[colspan='2']").Length = 0 Then
            outputRow = outputRow + 1: outputColumn = 1
            For c = 0 To row.getElementsByTagName("td").Length - 1
                results(outputRow, outputColumn) = row.getElementsByTagName("td")(c).innerText
                outputColumn = outputColumn + 1
            Next
        End If
        Set row = Nothing
    Next

    results = Application.Transpose(results)
    ActiveSheet.Cells(1, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
End Sub
选项显式
公共子可转置()
Dim xhr为MSXML2.XMLHTTP60,html为MSHTML.HTMLDocument,表格为MSHTML.htmltable
'必需的VBE(Alt+F11)>工具>参考>Microsoft HTML对象库;Microsoft XML,v6(您的版本可能有所不同)
设置xhr=New MSXML2.XMLHTTP60
Set html=New MSHTML.HTMLDocument
'7NXBG2;8QT2E3
使用xhr
.打开“获取”https://www.chrono24.com/watch/8QT2E3”“错
.发送
html.body.innerHTML=.responseText
以
Set table=html.querySelector(“.specifications table”)
Dim results(),rowCountToExclude尽可能长
rowCountToExclude=html.queryselectoral(“.specifications table[colspan='2']”)。长度
ReDim结果(1到table.rows.Length-rowCountToExclude,1到table.getElementsByTagName(“tr”)(0)、Children(0)、getAttribute(“colspan”))
Dim r为Long,c为Long,outputRow为Long,outputColumn为Long,html2为MSHTML.HTMLDocument
设置html2=New MSHTML.HTMLDocument
对于r=0的table.getElementsByTagName(“tr”).Length-1
将行变暗为对象
Set row=table.getElementsByTagName(“tr”)(r)
html2.body.innerHTML=“&row.outerHTML&”
如果html2.querySelectorAll(“[colspan='2']”)长度=0,则
outputRow=outputRow+1:outputColumn=1
对于c=0到row.getElementsByTagName(“td”).Length-1
结果(outputRow,outputColumn)=行.getElementsByTagName(“td”)(c).innerText
outputColumn=outputColumn+1
下一个
如果结束
设置行=无
下一个
结果=应用程序。转置(结果)
单元格(1,1)。调整大小(UBound(results,1),UBound(results,2))=结果
端接头

您可以共享url吗?或者至少说明输入格式和预期输出格式以及相关html?您可以使用代码段工具通过(看起来像带on的图标)包含相关的html谢谢您的反馈。我已经附上了链接你想要整个细节表,因为它出现在页面上,但转置?感谢快速反馈!是的,我需要所有的信息。我们可以忽略标题吗,例如“基本信息”?如果没有,您希望他们如何出现?我非常感谢您的及时回复!我肯定我是个笨蛋。但是如果我使用编译错误代码,您需要在vbe中添加适当的引用,该引用在代码顶部指示所需的vbe(Alt+F11)>工具>引用>Microsoft HTML对象库;Microsoft XML,v6(您的版本可能会有所不同)错误消息是什么?它发生在哪一行?现在,真正的问题是什么(当然是我的错误!!!)我启动了一个新模块,现在它工作得很好!!!非常感谢穆奇!好的,当我尝试使用相同的步骤,但我更改了相同页面的url时,例如:“”。我的驾驶错误是“9”-为什么?