英文:
Web Scraping Table into Excel Worksheet
问题
下午好,
首先...我知道现在不再支持使用IE浏览器。
我已经找到了如何抓取表格数据,但我需要将表格数据放在A5单元格中。我尝试在代码的某些部分添加.range("A5"),但无法使其工作。请参见下面的代码:
Private Sub CommandButton3_Click()
'清除抓取之前的范围
ActiveSheet.Range("A5:k5000").ClearContents
'导航到网页
Dim ie As Object
Dim url As String
url = "https://www.myfueltanksolutions.com/validate.asp"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate url
Do While ie.Busy: DoEvents: Loop
Do Until ie.readyState = 4: DoEvents: Loop
'登录凭据并提交
Dim idoc As MSHTML.HTMLDocument
Set idoc = ie.document
idoc.all.CompanyID.Value = "CompanyID"
idoc.all.UserId.Value = "UserID"
idoc.all.Password.Value = "Password"
idoc.parentWindow.execScript "submitForm();"
Do While ie.Busy: DoEvents: Loop
Do Until ie.readyState = 4: DoEvents: Loop
'抓取表格
Dim tbl As HTMLTable
Set tbl = ie.document.getElementById("RecentInventorylistform")
Dim rowcounter As Integer
Dim colcounter As Integer
rowcounter = 1
colcounter = 1
Dim tr As HTMLTableRow
Dim td As HTMLTableCell
Dim th
Dim mySh As Worksheet
Set mySh = ThisWorkbook.Sheets("Sheet1")
For Each tr In tbl.getElementsByTagname("tr")
'循环遍历表格单元格
For Each td In tr.getElementsByTagname("td")
mySh.Cells(rowcounter, colcounter).Value = td.innerText
colcounter = colcounter + 1
Next td
colcounter = 1
rowcounter = rowcounter + 1
Next tr
'注销并关闭网站
ie.navigate ("https://www.myfueltanksolutions.com/signout.asp?action=rememberlogin")
ie.Quit
'最后更新和完成时的消息框
Range("N1") = Now()
MsgBox "数据导入成功。按“确定”继续。"
End Sub
非常感谢您的帮助!
英文:
Good afternoon,
First off....I know using ie for anything isn't great since its not being supported anymore.
I have figured out how to scrape a table but I need to have the table data to be placed in cell A5. I tried adding .range("A5") to parts of the code but haven't been able to get it to work. Please see code below:
Private Sub CommandButton3_Click()
'Clear the range before scraping
ActiveSheet.Range("A5:k5000").ClearContents
'Navigating to webpage
Dim ie As Object
Dim url As String
url = "https://www.myfueltanksolutions.com/validate.asp"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate url
Do While ie.Busy: DoEvents: Loop
Do Until ie.readyState = 4: DoEvents: Loop
'Login credentails and submit
Dim idoc As MSHTML.HTMLDocument
Set idoc = ie.document
idoc.all.CompanyID.Value = "CompanyID"
idoc.all.UserId.Value = "UserID"
idoc.all.Password.Value = "Password"
idoc.parentWindow.execScript "submitForm();"
Do While ie.Busy: DoEvents: Loop
Do Until ie.readyState = 4: DoEvents: Loop
'Scrapging table
Dim tbl As HTMLTable
Set tbl = ie.document.getElementById("RecentInventorylistform")
Dim rowcounter As Integer
Dim colcounter As Integer
rowcounter = 1
colcounter = 1
Dim tr As HTMLTableRow
Dim td As HTMLTableCell
Dim th
Dim mySh As Worksheet
Set mySh = ThisWorkbook.Sheets("Sheet1")
For Each tr In tbl.getElementsByTagname("tr")
'Loop thru table cells
For Each td In tr.getElementsByTagname("td")
mySh.Cells(rowcounter, colcounter).Value = td.innerText
colcounter = colcounter + 1
Next td
colcounter = 1
rowcounter = rowcounter + 1
Next tr
'Log out and close website
ie.navigate ("https://www.myfueltanksolutions.com/signout.asp?action=rememberlogin")
ie.Quit
'Last updated and message box at completion
Range("N1") = Now()
MsgBox "Data Imported Successfully. Press Ok to Continue."
End Sub
Thank you so much for the help!
答案1
得分: 1
你想要从单元格 A5 开始吗?如果是的话,你应该更改mySh.Cells(rowcounter, colcounter).Value中的值。单元格 A5 是 Cells(5, 1),所以你应该从 Cells(5, 1) 开始。你可以尝试像这样更改代码:
Dim rowcounter As Integer
Dim colcounter As Integer
rowcounter = 5
colcounter = 1
英文:
Do you want to start from cell A5? If so, you should change the value in mySh.Cells(rowcounter, colcounter).Value. Cell A5 is Cells(5, 1), so you should start from Cells(5, 1). You can try to change the code like this:
Dim rowcounter As Integer
Dim colcounter As Integer
rowcounter = 5
colcounter = 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论