Excel VBA代码从网站获取数据

时间:2018-06-23 09:48:48

标签: excel vba web-scraping

我已经开发了以下代码,以便从网页(内部IT支持)中获取一些信息以转换为excel,包括:票证ID,描述,内容,人员,首次回复以及所有可能的信息,以便进行报告。根据我们的KPI,现在我对编码尤其是VBA并不了解,下面的代码每次运行都获取一条信息,因此我无法获得所有票证及其信息的列表。

请帮助我完成方法或代码。

干杯:)

Private sub report_data_click ()
Dim IE as InternetExplorer
Dim HTML as variant
set IE = createobject("internetexplorer.application")
IE.visible = false
ie.navigate "https://forum.fortinet.com"
while IE.busy
 do events
wend
while ie.readystate < 4
do events
wend
set HTML = ie.document 
set elements as variant 
set elements2 as variant
set elements3 as variant 
set elements4 as variant 
set elements = html.getelementsbyclassname ("contentlink")
for i=0 to elements.length - 1
sheet1.range ("A" & (9+1)) =elements(i).innertext
next i

set elements2 = html.getelementsbyclassname ("authorlink")
for i=0 to elements2.length - 1
sheet1.range ("A" & (9+1)) =elements2(i).innertext
next i

set elements3 = html.getelementsbyclassname ("performdateformat")
for i=0 to elements3.length - 1
sheet1.range ("A" & (9+1)) =elements3(i).innertext
next i

set elements4 = html.getelementsbyclassname ("Messagelink newpost")
for i=0 to elements4.length - 1
sheet1.range ("A" & (9+1)) =elements4(i).innertext
next i
IE.quit
set IE = Nothing 
End sub

1 个答案:

答案 0 :(得分:0)

似乎您来自vb.net之类的不同语言,因为您编写上述脚本的方式使我想起了这种东西。您只是不能将所有内容(特别是对象)声明为 :loop powershell.exe -ExecutionPolicy Bypass -File c:\ps\lockedlist.ps1 powershell.exe -ExecutionPolicy Bypass -File c:\ps\unlock.ps1 cls goto loop import-module activedirectory search-adaccount -lockedout | select name, samaccountname, OU $user = Read-Host "Enter user account (SAMACCOUNTNAME) to unlock or press ENTER to refresh list" Search-ADAccount -LockedOut | Where {$_.samaccountname -eq $user} | Unlock-ADAccount ,就像您编写的那样。尝试以下方法从类名variant下的每个容器中获取数据。我试图创建一个脚本,该脚本应该解析每个容器中的三个字段(在脚本中提到)。如果您要添加任何内容,我认为您可以按照以下实现的逻辑进行管理。

Set

添加到库中以执行上述脚本的参考:

newActiveList-item