Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何使用VBA一次刮取多个页面/链接?_Html_Excel_Vba_Internet Explorer_Web Scraping - Fatal编程技术网

Html 如何使用VBA一次刮取多个页面/链接?

Html 如何使用VBA一次刮取多个页面/链接?,html,excel,vba,internet-explorer,web-scraping,Html,Excel,Vba,Internet Explorer,Web Scraping,我现在正试着从中获取信息。我的目标是让excel在新的选项卡中打开所有帖子,然后我想从每个页面中获取信息,因为起始页面没有那么多信息 在过去的几个小时里,我一直在试图弄清楚这一点,但我承认我对如何做感到非常困惑,只是总体上不确定下一步该做什么,所以任何指点都将不胜感激 这是我当前的代码,它运行得很正常,但正如我所说的,我不确定下一步该怎么做才能逐个打开它找到的链接,并在每个页面上搜索数据。 链接从第一页上刮下来,然后立即添加到我的电子表格中,但如果可能的话,我想跳过这一步,一次将它们全部刮下来

我现在正试着从中获取信息。我的目标是让excel在新的选项卡中打开所有帖子,然后我想从每个页面中获取信息,因为起始页面没有那么多信息

在过去的几个小时里,我一直在试图弄清楚这一点,但我承认我对如何做感到非常困惑,只是总体上不确定下一步该做什么,所以任何指点都将不胜感激

这是我当前的代码,它运行得很正常,但正如我所说的,我不确定下一步该怎么做才能逐个打开它找到的链接,并在每个页面上搜索数据。 链接从第一页上刮下来,然后立即添加到我的电子表格中,但如果可能的话,我想跳过这一步,一次将它们全部刮下来

谢谢!:)

Sub-GetData()
Dim objIE作为InternetExplorer
Dim itemEle作为对象
Dim upvote为整数,awards为整数,动画为整数
Dim postdate为字符串、upvotepercent为字符串、oc为字符串、filetype为字符串、linkurl为字符串、myhtmldata为字符串、visiComments为字符串、totalComments为字符串、RemoveComments为字符串
Dim y作为整数
Set objIE=新的InternetExplorer
objIE.Visible=False
objIE.navigate(ActiveCell.Value)
Do While objIE.Busy=True或objIE.readyState 4:DoEvents:Loop
y=1
对于objIE.document.getElementsByClassName(“平面列表按钮”)中的每个itemEle
visiComments=itemEle.getElementsByTagName(“a”)(0.innerText
linkurl=itemEle.getElementsByTagName(“a”)(0.href
图纸(“图纸1”)。范围(“A”和“y”)。值=注释
图纸(“图纸1”)。范围(“B”和“y”)。值=链接URL
y=y+1
下一个

End Sub
您应该能够收集URL,然后在循环中访问,并将访问页面的结果写入数组,然后将数组写入工作表。将其添加到现有行之后

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
注意:由于VBA是单线程的,因此您只可能获得页面加载。要做到这一点,您需要存储对每个选项卡的引用,或者首先打开所有选项卡,然后在相关打开的窗口中循环进行刮取。老实说,我更喜欢保持同一张账单

Set nodeList = ie.document.querySelectorAll(".comments")
Redim urls(0 To nodeList.Length-1)
Redim results(1 to nodeList.Length, 1 to 3)
'Store all urls in an array to later loop
For i = 0 To nodeList.Length -1 
    urls(i) = nodeList.item(i).href
Next

For i = LBound(urls) To UBound(urls)
    ie.Navigate2   urls(i)
    While ie.Busy Or ie.Readystate <> 4: DoEvents:Wend
    'may need a pause here
    results(i + 1, 1) = ie.document.querySelector("a.title").innerText 'title
    results(i + 1, 2) = ie.document.querySelector(".number").innerText 'upvotes
    results(i + 1, 3) = ie.document.querySelector(".word").NextSibling.nodeValue '%
Next
ActiveSheet.Cells(1,1).Resize(UBound(results,1) , UBound(results,2)) = results
Set nodeList=ie.document.querySelectorAll(“.comments”)
重拨URL(0到节点列表。长度-1)
Redim结果(1到节点列表长度,1到3)
'将所有URL存储在数组中以供以后循环
对于i=0到节点列表长度-1
URL(i)=nodeList.item(i).href
下一个
对于i=LBound(URL)到UBound(URL)
ie.navigate2url(i)
当ie忙或ie准备时状态4:DoEvents:Wend
“可能需要在这里暂停一下
结果(i+1,1)=即.document.querySelector(“a.title”).innerText的标题
结果(i+1,2)=即.document.querySelector(“.number”).innerText'upvows
结果(i+1,3)=ie.document.querySelector(“.word”).NextSibling.nodeValue'%
下一个
单元格(1,1).Resize(UBound(results,1),UBound(results,2))=结果

@QHarr我基本上是试图打开每个链接(HREF),然后为每个链接刮取一些html元素,并将它们输出到我的电子表格中。因此,要抓取的数据可以是,例如,#upvots和输出将是一个数字。是的,%Upvoted是这些页面唯一的附加信息,但它对我的项目非常重要,我只是尽可能地自动化。是的!因为这个百分比让我陷入困境,真的。
.NodeValue
的工作原理是否与
类似。下一个兄弟姐妹在beautifulsou@QHarr中工作?对不起,如果我花时间回复,我只是想理解,而不仅仅是复制^^^^出于某种原因,它正在删除列表中第一篇文章的标题,以及上面的投票,但不是%的人。然后在宏结束后,我会看到第一篇文章(及其向上投票)重复了25行,而不是所有不同的文章。我不知道是什么原因造成的。我检查了HTML,还有一个CSS类叫做“word”,技术上低于我想要的那个,这可能是导致%出现问题的原因,虽然这可能不是它不删除其他帖子的原因。这解决了第一个问题,谢谢!是的,它在写[对象文本]。奇怪的是,它告诉我“对象不支持这个属性或方法”。
Set nodeList = ie.document.querySelectorAll(".comments")
Redim urls(0 To nodeList.Length-1)
Redim results(1 to nodeList.Length, 1 to 3)
'Store all urls in an array to later loop
For i = 0 To nodeList.Length -1 
    urls(i) = nodeList.item(i).href
Next

For i = LBound(urls) To UBound(urls)
    ie.Navigate2   urls(i)
    While ie.Busy Or ie.Readystate <> 4: DoEvents:Wend
    'may need a pause here
    results(i + 1, 1) = ie.document.querySelector("a.title").innerText 'title
    results(i + 1, 2) = ie.document.querySelector(".number").innerText 'upvotes
    results(i + 1, 3) = ie.document.querySelector(".word").NextSibling.nodeValue '%
Next
ActiveSheet.Cells(1,1).Resize(UBound(results,1) , UBound(results,2)) = results