无法以正确的方式在 querySelectorAll 容器中使用 querySelector
Unable to use querySelector within querySelectorAll container in the right way
我想知道如何在 .querySelectorAll()
上使用 .querySelector()
。
例如,当我这样尝试时,我得到了预期的结果:
Sub GetContent()
Const URL$ = "https://whosebug.com/questions/tagged/web-scraping?tab=Newest"
Dim HTMLDoc As New HTMLDocument
Dim HTML As New HTMLDocument, R&, I&
With New XMLHTTP60
.Open "Get", URL, False
.send
HTMLDoc.body.innerHTML = .responseText
End With
With HTMLDoc.querySelectorAll(".summary")
For I = 0 To .Length - 1
HTML.body.innerHTML = .Item(I).outerHTML
R = R + 1: Cells(R, 1).Value = HTML.querySelector(".question-hyperlink").innerText
Next I
End With
End Sub
即使我使用相同的逻辑,当我选择另一个站点以获取 table 中可用的 Rank
列下的值时,脚本不再起作用:
Sub GetContent()
Const URL$ = "https://www.worldathletics.org/records/toplists/sprints/100-metres/outdoor/men/senior/2020?page=1"
Dim HTMLDoc As New HTMLDocument
Dim HTML As New HTMLDocument, R&, I&
With New XMLHTTP60
.Open "Get", URL, False
.send
HTMLDoc.body.innerHTML = .responseText
End With
With HTMLDoc.querySelectorAll("#toplists tbody tr")
For I = 0 To .Length - 1
HTML.body.innerHTML = .Item(I).outerHTML
R = R + 1: Cells(R, 1).Value = HTML.querySelector("td").innerText
Next I
End With
End Sub
这是我正在谈论的两个脚本中的 Cells(R, 1).Value = HTML.querySelector().innerText
行。我在这个容器中使用相同的 .querySelectorAll()
.
如果我在 .getElementsByTagName()
上使用 .querySelector()
,我发现它有效。我还发现在 TagName
上使用 TagName
或在 ClassName
e.t.c 上使用 ClassName
成功。所以,我可以通过几种不同的方式获取内容。
如何在第二个脚本的 .querySelectorAll()
上使用 .querySelector()
才能使其正常工作?
将其包装在 table 标记中,以便 html 解析器知道如何处理它。
HTML.body.innerHTML = "<table>" & .Item(I).outerHTML & "</table>"
这样做会保留开始的 td 标签的结构,否则会去掉“<”。
我想知道如何在 .querySelectorAll()
上使用 .querySelector()
。
例如,当我这样尝试时,我得到了预期的结果:
Sub GetContent()
Const URL$ = "https://whosebug.com/questions/tagged/web-scraping?tab=Newest"
Dim HTMLDoc As New HTMLDocument
Dim HTML As New HTMLDocument, R&, I&
With New XMLHTTP60
.Open "Get", URL, False
.send
HTMLDoc.body.innerHTML = .responseText
End With
With HTMLDoc.querySelectorAll(".summary")
For I = 0 To .Length - 1
HTML.body.innerHTML = .Item(I).outerHTML
R = R + 1: Cells(R, 1).Value = HTML.querySelector(".question-hyperlink").innerText
Next I
End With
End Sub
即使我使用相同的逻辑,当我选择另一个站点以获取 table 中可用的 Rank
列下的值时,脚本不再起作用:
Sub GetContent()
Const URL$ = "https://www.worldathletics.org/records/toplists/sprints/100-metres/outdoor/men/senior/2020?page=1"
Dim HTMLDoc As New HTMLDocument
Dim HTML As New HTMLDocument, R&, I&
With New XMLHTTP60
.Open "Get", URL, False
.send
HTMLDoc.body.innerHTML = .responseText
End With
With HTMLDoc.querySelectorAll("#toplists tbody tr")
For I = 0 To .Length - 1
HTML.body.innerHTML = .Item(I).outerHTML
R = R + 1: Cells(R, 1).Value = HTML.querySelector("td").innerText
Next I
End With
End Sub
这是我正在谈论的两个脚本中的 Cells(R, 1).Value = HTML.querySelector().innerText
行。我在这个容器中使用相同的 .querySelectorAll()
.
如果我在 .getElementsByTagName()
上使用 .querySelector()
,我发现它有效。我还发现在 TagName
上使用 TagName
或在 ClassName
e.t.c 上使用 ClassName
成功。所以,我可以通过几种不同的方式获取内容。
如何在第二个脚本的 .querySelectorAll()
上使用 .querySelector()
才能使其正常工作?
将其包装在 table 标记中,以便 html 解析器知道如何处理它。
HTML.body.innerHTML = "<table>" & .Item(I).outerHTML & "</table>"
这样做会保留开始的 td 标签的结构,否则会去掉“<”。