获取<div>对象全部innerText及提取指定页面div内容的VBA技术求助
VBA Solution to Extract Target Div Content
Got it, let's fix this up for you. Below is the updated code that not only navigates to your target results page but also extracts all content from <div> elements with the class processosJurisprudenciaAcordaos. I'll also break down how to grab the full innerText from each div.
Sub tese() Dim ie As InternetExplorer Dim pesquisa As String Dim elemUnique, elemCollection As Object Dim targetDivs As Object Dim i As Integer ' Get user search input pesquisa = InputBox("Digite os termos que quer pesquisar: ", "", "") If pesquisa = "" Then Exit Sub ' Exit if user cancels or leaves input empty Set ie = New InternetExplorer ie.Visible = True ' Navigate to initial search page ie.navigate "http://www.stf.jus.br/portal/jurisprudencia/pesquisarJurisprudencia.asp" ieBusy ie ' Fill and submit the search form ie.document.getElementById("txtPesquisaLivre").innerText = pesquisa ie.document.getElementById("pesquisar").Click ieBusy ie ' Click the first "linkPagina" to reach results (matches your original logic) Set elemCollection = ie.document.getElementsByTagName("a") For Each elemUnique In elemCollection If elemUnique.className Like "linkPagina" Then elemUnique.Click Exit For End If Next elemUnique ieBusy ie ' Extract all divs with the target class Set targetDivs = ie.document.getElementsByClassName("processosJurisprudenciaAcordaos") ' Handle results If targetDivs.Length > 0 Then ' Example: Output content to Excel's Sheet1 (adjust as needed) For i = 0 To targetDivs.Length - 1 ' Write full innerText of each div to a new row ThisWorkbook.Sheets("Sheet1").Cells(i + 1, 1).Value = targetDivs(i).innerText ' Optional: Add a separator between entries for readability ThisWorkbook.Sheets("Sheet1").Cells(i + 2, 1).Value = "------------------------" Next i MsgBox "Extração concluída! Verifique a Planilha 1 para os resultados.", vbInformation Else MsgBox "Nenhum div com classe 'processosJurisprudenciaAcordaos' foi encontrado.", vbExclamation End If ' Cleanup resources ie.Quit Set ie = Nothing Set targetDivs = Nothing Set elemCollection = Nothing End Sub ' Helper function to wait for IE to finish loading critical pages Sub ieBusy(ie As InternetExplorer) Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE DoEvents Loop End Sub
Key Details You Asked For:
- Grabbing Target Divs: The line
Set targetDivs = ie.document.getElementsByClassName("processosJurisprudenciaAcordaos")pulls all elements with your specified class into a collection you can loop through. - Extracting Full InnerText: The
innerTextproperty of each div returns all text content inside the element (including text from child elements), with all HTML tags stripped out. This gives you the complete, readable content of each div exactly as it appears on the page. - Waiting for Page Loads: The
ieBusyfunction ensures we don't try to interact with elements before the page finishes loading—this prevents runtime errors that are common with web-scraping VBA. - Output Flexibility: The example writes results to Excel, but you can modify this section to save to a text file (using
Open/Printstatements) or another format that fits your workflow.
Quick Setup Note:
Make sure you have the Microsoft Internet Controls reference enabled in VBA. Go to Tools > References and check the box for it—this is required for the InternetExplorer object to work.
内容的提问来源于stack exchange,提问作者pedro tedesco
相关产品推荐
相关产品推荐

