HTML表格刮使用VBA
IM试图从tr标签的国家,但它只是给我的表的第一行我怎么刮的特定行HTML表格刮使用VBA
Sub ipsearch() Dim x As Integer
x = 2
Do Until x = 4000
Dim ie As New InternetExplorer
ie.navigate "https://whatismyipaddress.com/ip/" & Range("E" & x).Value
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Dim doc As HTMLDocument
Set doc = ie.document
Dim sDD As String
sDD = Trim(doc.getElementsByTagName("td")(, 0).innerText)
Range("F" & x).Value = sDD
x = x + 1
Loop
End Sub
蓝色位是什么我得到和黄是什么,我想
回答:
这是后话,将在您的例子返回“联合王国”:
Sub ipsearch() Dim x As Long
x = 2
Do Until x = 4000
Dim ie As New InternetExplorer
ie.navigate "https://whatismyipaddress.com/ip/" & "2.99.247.66"
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Dim doc As HTMLDocument
Set doc = ie.document
Dim sDD As String
ie.Visible = True
sDD = Trim(doc.getElementsByTagName("tbody")(1).innerText)
Range("F" & x).Value = Split(sDD, vbCrLf)(5)
x = x + 1
Loop
End Sub
一般情况下,自动对焦新编写更好的代码的想法。
- 真的很高兴知道你在用什么 - 例如,在你的截图中,来自IP的数字取自蓝色以上的行,例如,从“2.99.247.66的详细信息”。
- 当您向StackOverflow提供代码时,请务必提及您的代码正在使用的其他库。你的情况这两种:
,或者确保您的代码使用后期绑定,因此图书馆不应该添加。 - 一般情况下,可以考虑使用Long
,在Integer
代替 - Why Use Integer Instead of Long?
- 格式代码,当你提交。它看起来好一点。 Ctrl + K是StackOverflow中的快捷方式。
- 对于VBA使用
Option Explicit
。 - 尝试硬编码问题中的变量。你的情况:
"https://whatismyipaddress.com/ip/" & "2.99.247.66"
- 最后但并非最不重要的,很多网站都不喜欢被cralwed。只是fyi - https://www.google.com/search?q=is+crawling+legal
回答:
给这个镜头。它会把你的国名带到Range(“A1”)。
Sub ipsearch() Dim IE As New InternetExplorer, html As HTMLDocument
Dim post As Object
With IE
.Visible = False
.navigate "https://whatismyipaddress.com/ip/2.99.247.66"
Do Until .readyState = READYSTATE_COMPLETE: Loop
Set html = .document
End With
For Each post In html.getElementsByTagName("th")
If InStr(post.innerText, "Country:") > 0 Then [A1] = post.ParentNode.LastChild.innerText: Exit For
Next post
IE.Quit
End Sub
引用添加到库中:
1. Microsoft Internet Controls 2. Microsoft HTML Object Library
,并使其方式速度更快,请尝试以下之一:
Sub ipsearch() Dim HTTP As New XMLHTTP60, html As New HTMLDocument
Dim post As Object
With HTTP
.Open "GET", "https://whatismyipaddress.com/ip/2.99.247.66", False
.send
html.body.innerHTML = .responseText
End With
For Each post In html.getElementsByTagName("th")
If InStr(post.innerText, "Country:") > 0 Then [A1] = post.ParentNode.LastChild.innerText: Exit For
Next post
End Sub
引用添加到库中:
1. Microsoft XML,v6.0 ''or the version you have 2. Microsoft HTML Object Library
产量:
United Kingdom
以上是 HTML表格刮使用VBA 的全部内容, 来源链接: utcz.com/qa/262654.html