【问题标题】:VBA Get webpage Link and Download File toVBA 获取网页链接并将文件下载到
【发布时间】:2020-02-28 23:01:43
【问题描述】:

很久以前就开始编程VBA了。我想在网络表中获取 coll um (x) 中的所有链接 PDF 文件。首先,我创建了登录名并导航到该站点以获取文件。
站点表:

Sub goToShopWH()

Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim clip As Object

'Table Grapping
Dim ieTable As Object

'create a new instance of ie
'Set ieApp = New InternetExplorer
Set ieApp = CreateObject("InternetExplorer.Application")

'you don’t need this, but it’s good for debugging
ieApp.Visible = True

'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "https://website.com/ishop/Home.html"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents:
Loop

' Login to site

Set ieDoc = ieApp.Document

'fill in the login form – View Source from your browser to get the control names
With ieDoc '.forms("loginForm_0")
 .getElementById("loginField").Value = "username"
 .getElementById("password").Value = "Password"
 .getElementById("loginSubmit").Click
  
 

Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

'now that we’re in, go to the page we want
'Switsh to search form

ieApp.Navigate "https://website.com/ishop/account/MyAccount,$comp$account$AccountNavigation.orderHistory.sdirect?sp=Saccount%2FOrderHistory"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

' fillout form

Set ieDoc = ieApp.Document
With ieDoc '.forms("Form_0")
 .getElementById("PropertySelection_2").Value = "7"
 .getElementById("commtxt").Value = "190055"  'Projekt Nummer oder Text.
 .getElementById("Submit_0").Click
  
End With


Do
DoEvents
Loop Until ieApp.ReadyState = 4

Application.Wait Now + TimeSerial(0, 0, 5)


With ieDoc '.forms("Form_1")
 .getElementById("PropertySelection").Value = "3"
 .getElementById("PropertySelection").FireEvent ("onchange")

End With

Do
DoEvents
Loop Until ieApp.ReadyState = 4
Application.Wait Now + TimeSerial(0, 0, 5)


End With

Set webpage = ieApp.Document

Set table_data = webpage.getElementsByTagName("tr")

End Sub

请帮我解决这个问题,让我想将表格导入 sheet2 并下载 ("Table"),("tbody")(tr)(1 to X),(td)(10),( href).点击

【问题讨论】:

  • 看看你是否可以直接从链接下载二进制或 urlmon,而不是点击链接,否则这将是一场噩梦。
  • 可以为mi做一个样本

标签: excel vba html-table href tr


【解决方案1】:

我想你是在追求这样的东西。

Sub webpage()

    Dim internet As Object
    Dim internetdata As Object
    Dim div_result As Object
    Dim header_links As Object
    Dim link As Object
    Dim URL As String

    Set internet = CreateObject("InternetExplorer.Application")
    internet.Visible = True

    URL = "https://www.google.co.in/search?q=how+to+program+in+vba"
    internet.Navigate URL

    Do Until internet.ReadyState >= 4
        DoEvents
    Loop

    Application.Wait Now + TimeSerial(0, 0, 5)

    Set internetdata = internet.Document
    Set div_result = internetdata.getelementbyid("res")


    Set header_links = div_result.getelementsbytagname("h3")

    For Each h In header_links
        Set link = h.ChildNodes.Item(0)
        Cells(Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = link.href
    Next

    MsgBox "done"
End Sub

或者,也许是这个。

' place your URL in cell L1
Sub HREF_Web()

Dim doc As HTMLDocument
Dim output As Object

Set IE = New InternetExplorer
IE.Visible = False
IE.navigate Range("L1")

Do
'DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE

Set doc = IE.document
Set output = doc.getElementsByTagName("a")

i = 5

For Each link In output
    'If link.InnerHTML = "" Then
        Range("A" & i).Value2 = link
   ' End If
   i = i + 1
Next

MsgBox "Done!"

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多