VBA 网页抓取问题 - 如何使用 html 结构(href / child/ )导航特定网页
VBA web scraping issue - how to navigate specific web using html structure (href / child/ )
亲爱的VBA同学您好:)
Sub login()
'test
Const URL$ = "https://kwm.kromi.de/cgi-bin/kwm?HTML=frontend/login.htm"
Dim UserName As String, Password As String, LoginData As Worksheet
Set LoginData = ThisWorkbook.Worksheets("Sheet1")
UserName = LoginData.Cells(1, "B").Value
Password = LoginData.Cells(2, "B").Value
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = False
.Navigate URL
ieBusy IE
.Visible = True
Dim oLogin As Object, oPassword As Object
Set oLogin = .document.getElementsByName("VS_LOGIN")(0)
Set oPassword = .document.getElementsByName("VS_PASSWORD")(0)
oLogin.Value = UserName
oPassword.Value = Password
.document.forms(0).submit
ieBusy IE
Stop
'.document.getElementsByTagName("a")(2).href
'.document.getElementsByClassName("link3").Click
.Navigate2 ""
ieBusy IE
Stop
End With
'''
End Sub
Sub ieBusy(IE As Object)
Do While IE.Busy Or IE.readyState < 4
DoEvents
Loop
End Sub
第一个任务是工作,宏登录网站。我需要更深入地点击一些东西,但是网络结构对我的小脑袋来说太多了我正在网站上查看一些示例,但没有任何效果。我在下面显示了网站代码。我需要点击按钮“statystyka”。
/html/body/div[1]/div[1]/a[2]
- Xpath 地址
[link图片]https://ibb.co/2Pgx2tn
请你帮帮我:)
编辑:
我试过使用这样的东西:
'.document.getElementsByTagName("a")(2).href
但这不是很好的思考方式
您需要移动到适当的框架,在我使用时添加等待。导航到框架 src
,然后您可以通过 onclick
属性的子字符串进行定位:
ie.navigate ie.document.querySelector("[name=Navigator]").src
ieBusy ie
ie.document.querySelector("[onclick*=statistic]").click
如果你想通过标签导航你需要如下设置框架
Dim doc As HTMLDocument
Dim doc2 As HTMLDocument
Dim lnk As HTMLLinkElement
Set doc = IE.document
Set doc2 = doc.frames("Navigator").document
Set lnk = doc2.getElementsByTagName("A")(1)
lnk.Click
@QHarr @Raymond Wu :) 感谢您尝试帮助,也许这将是其他人的解决方案
亲爱的VBA同学您好:)
Sub login()
'test
Const URL$ = "https://kwm.kromi.de/cgi-bin/kwm?HTML=frontend/login.htm"
Dim UserName As String, Password As String, LoginData As Worksheet
Set LoginData = ThisWorkbook.Worksheets("Sheet1")
UserName = LoginData.Cells(1, "B").Value
Password = LoginData.Cells(2, "B").Value
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = False
.Navigate URL
ieBusy IE
.Visible = True
Dim oLogin As Object, oPassword As Object
Set oLogin = .document.getElementsByName("VS_LOGIN")(0)
Set oPassword = .document.getElementsByName("VS_PASSWORD")(0)
oLogin.Value = UserName
oPassword.Value = Password
.document.forms(0).submit
ieBusy IE
Stop
'.document.getElementsByTagName("a")(2).href
'.document.getElementsByClassName("link3").Click
.Navigate2 ""
ieBusy IE
Stop
End With
'''
End Sub
Sub ieBusy(IE As Object)
Do While IE.Busy Or IE.readyState < 4
DoEvents
Loop
End Sub
第一个任务是工作,宏登录网站。我需要更深入地点击一些东西,但是网络结构对我的小脑袋来说太多了我正在网站上查看一些示例,但没有任何效果。我在下面显示了网站代码。我需要点击按钮“statystyka”。
/html/body/div[1]/div[1]/a[2]
- Xpath 地址
[link图片]https://ibb.co/2Pgx2tn
请你帮帮我:)
编辑:
我试过使用这样的东西:
'.document.getElementsByTagName("a")(2).href
但这不是很好的思考方式
您需要移动到适当的框架,在我使用时添加等待。导航到框架 src
,然后您可以通过 onclick
属性的子字符串进行定位:
ie.navigate ie.document.querySelector("[name=Navigator]").src
ieBusy ie
ie.document.querySelector("[onclick*=statistic]").click
如果你想通过标签导航你需要如下设置框架
Dim doc As HTMLDocument
Dim doc2 As HTMLDocument
Dim lnk As HTMLLinkElement
Set doc = IE.document
Set doc2 = doc.frames("Navigator").document
Set lnk = doc2.getElementsByTagName("A")(1)
lnk.Click
@QHarr @Raymond Wu :) 感谢您尝试帮助,也许这将是其他人的解决方案