Excel 从VBA获取所有内部文本
如何从网页中获取所有内部文本使用下面的代码仅获取第一行,Excel 从VBA获取所有内部文本,excel,vba,Excel,Vba,如何从网页中获取所有内部文本使用下面的代码仅获取第一行,“段落”标记停止读取。我可以添加vCompanyCity等-但不知道如何获得此框中的下一行 Set ie = CreateObject("InternetExplorer.application") ie.navigate ("https://www.xxxx.com/") While ie.Busy Or ie.ReadyState <> 4: DoEvents: Wend vCompanyAddress(i - 1) = i
“段落”标记停止读取。我可以添加vCompanyCity等-但不知道如何获得此框中的下一行
Set ie = CreateObject("InternetExplorer.application")
ie.navigate ("https://www.xxxx.com/")
While ie.Busy Or ie.ReadyState <> 4: DoEvents: Wend
vCompanyAddress(i - 1) = ie.document.all("header-left-box").innerText
more code....
End Sub
Set ie=CreateObject(“InternetExplorer.application”)
例如,导航(“https://www.xxxx.com/")
当ie忙或ie ReadyState 4:DoEvents:Wend时
vCompanyAddress(i-1)=ie.document.all(“标题左框”).innerText
更多代码。。。。
端接头
试一试这样的东西。你的帖子中缺少一些细节,所以我不得不做出一些假设 以下有两种方法: 1) 使用getElementByID并查看InnerText是否返回 2) 使用getElementByID,然后迭代段落标记
Public Sub test()
Dim ie As Object
Dim vCompanyAddress As Variant
Dim i As Long: i = 0
Dim Elements As Object
Dim Element As Object
ReDim vCompanyAddress(1000) ' Not sure how big this array should be
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate ("https://www.xxxx.com/")
While .Busy Or .ReadyState <> 4: DoEvents: Wend
'You can try two things, this:
vCompanyAddress(i) = .document.getElementById("header-left-box").innerText
'Or you can try this, get the element then create an element
'collection with all paragraphs tags
Set Elements = .document.getElementById("header-left-box").getElementsByTagName("p")
For Each Element In Elements
vCompanyAddress(i) = Element.innerText
i = i + 1
Next
End With
End Sub
公共子测试()
模糊的物体
Dim vCompanyAddress作为变量
长度为的尺寸i:i=0
将图元变暗为对象
作为对象的暗元素
ReDim vCompanyAddress(1000)'不确定此阵列应多大
设置ie=CreateObject(“InternetExplorer.Application”)
与ie
.导航(“https://www.xxxx.com/")
忙时或准备时状态4:DoEvents:Wend
“你可以尝试两件事,这是:
vCompanyAddress(i)=.document.getElementById(“标题左框”).innerText
'或者您可以尝试此操作,获取元素,然后创建一个元素
'包含所有段落标记的集合
Set Elements=.document.getElementById(“标题左框”).getElementsByTagName(“p”)
对于元素中的每个元素
vCompanyAddress(i)=Element.innerText
i=i+1
下一个
以
端接头
以下是我用来查找所有标签的方法示例,我需要这些标签来抓取、单击并将信息从网页发送给我自己,以及登录和执行其他常规过程,如输入信息,而不必手动操作。有了它,您可以使用instr语句构建更复杂的过程,以确定要搜索、单击等的内容,而无需手动执行任何操作,例如,如果microsoft或其他公司不断更改java脚本按钮标记,则可以使用文本字符串和偏移量来单击按钮,无论其名称/标记是什么
Sub BIG_EskimoRoll()
On Error Resume Next
Dim ExpR As Object
Set ExpR = ie.Document.getElementsByTagName("p")
i = 0
While i < ExpR.Length
If ExpR(i).Name <> "" Then
If ExpR(i).className = "expireFeature" Then msg = ExpR(i).innerText
''''this is good code to keep around'''''''''''
'If ExpR(i).className = "expireFeature" Then Debug.Print ExpR(i).className
'If ExpR(i).className = "expireFeature" Then Debug.Print ExpR(i).innerText
' Set text for search
Debug.Print "Ptsn: " & i
Debug.Print "Nm: " & ExpR(i).Name
Debug.Print "Type: " & ExpR(i).Type
Debug.Print "Vl: " & ExpR(i).Value
Debug.Print "ID: " & ExpR(i).ID
Debug.Print "inTxt: " & ExpR(i).innerText
Debug.Print "inHTML: " & ExpR(i).innerHTML
Debug.Print "outHTML: " & ExpR(i).outerHTML
Debug.Print "cNm: " & ExpR(i).className
Debug.Print "tNm: " & ExpR(i).tagName
Debug.Print "href: " & ExpR(i).href
End If
i = i + 1
Wend
End Sub
Sub BIG_EskimoRoll()
出错时继续下一步
作为对象的Dim ExpR
Set ExpR=ie.Document.getElementsByTagName(“p”)
i=0
而我
您是否尝试过GetElementById
方法?e、 g.vCompanyAddress(i-1)=ie.document.body.getelementbyid(“标题左框”).innerText
。是否确定字符串中没有多行由vbCrLF或vbLf字符分隔的文本?运行时错误“438”:对象不支持此属性或方法。然而,悲伤的面孔;现在我清醒了——它确实捕捉到了一切,我把结果写到Excel中的一个单元格中。展开公式栏以查看整个单元格显示单元格内的CRLF。一直以来都是这样,当我把问题贴出来的时候,我简直是哑口无言。