无法在 vba 中以正确的方式使用 querySelector [英] Can't use querySelector in the right way in vba
问题描述
我已经使用 vba 编写了一些代码,以便从 Torrent 站点的特定网页中获取所有电影名称.但是,按F8"我可以发现代码运行良好并打印结果,直到它到达该页面的最后一个结果.一旦到达要解析的姓氏,程序就会崩溃.我做了几次并遭受了同样的后果.如果 vba 不支持这种 css 选择器方法,那么我如何在最后一个之前收集结果?在执行之前是否有任何引用添加到库中或其他内容中?对此的任何帮助将不胜感激.
I've written some code using vba to get all the movie names from a specific webpage out of a torrent site. However, pressing "F8" I could find out that the code works well and prints the results until it hits the last result from that page. As soon as it reaches the last name to parse, the program crashes. I did several times and suffered the same consequences. If vba doesn't support this css selector method then how could I collect results before the last one? Is there any reference to add in the library or something else before execution? Any help on this will be vastly appreciated.
这是我写的代码:
Sub Torrent_data()
Dim http As New XMLHTTP60, html As New HTMLDocument
Dim movie_name As Object, movie As Object
With http
.Open "GET", "https://www.yify-torrent.org/search/1080p/", False
.send
html.body.innerHTML = .responseText
End With
Set movie_name = html.querySelectorAll("div.mv h3 a")
For Each movie In movie_name
x = x + 1: Cells(x, 1) = movie.innerText
Next movie
End Sub
推荐答案
试试这个:
Sub Torrent_data()
Dim http As New XMLHTTP60, html As New HTMLDocument, x As Long
With http
.Open "GET", "https://www.yify-torrent.org/search/1080p/", False
.send
html.body.innerHTML = .responseText
End With
Do
x = x + 1
On Error Resume Next
Cells(x, 1) = html.querySelectorAll("div.mv h3 a")(x - 1).innerText
Loop Until Err.Number = 91
End Sub
这是另一种不需要错误处理程序的方法:
This is another way which doesn't need error handler:
Sub GetContent()
Const URL$ = "https://yify-torrent.cc/search/1080p/"
Dim HTMLDoc As New HTMLDocument, R&, I&
With New ServerXMLHTTP60
.Open "Get", URL, False
.send
HTMLDoc.body.innerHTML = .responseText
End With
With HTMLDoc.querySelectorAll("h3 > a.movielink")
For I = 0 To .Length - 1
R = R + 1: Cells(R, 1).Value = .Item(I).innerText
Next I
End With
End Sub
这篇关于无法在 vba 中以正确的方式使用 querySelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!