vba code to get data from firefox and paste in exc

2019-09-03 15:06发布

i am trying to get some data from a website , i can only use Firefox to access the site and i need to get the data and paste it in excel , there are way to do it in internet explore but i cant sort out for Firefox. Can anyone help me with it ? below mentioned website is for sample .original website is internal network access, website will consist of data in a table. Thanks in advance

Sub Test_OpenFireFoxNewTab()
OpenInFireFoxNewTab "https://www.amazon.com"
End Sub

Sub OpenInFireFoxNewTab(url As String)
Dim pathFireFox As String
Dim ffDoc As Object
Dim ffApp As WebBrowser_V1
Dim ffTable As Object
Dim clip As DataTable
pathFireFox = "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
If Dir(pathFireFox) = "" Then pathFireFox = "C:\Program Files\Mozilla 
Firefox\firefox.exe"
If Dir(pathFireFox) = "" Then
MsgBox "FireFox Path Not Found", vbCritical, "Macro Ending"
Exit Sub
End If
Shell """" & pathFireFox & """" & " -new-tab " & url, vbHide

Set ffApp = New WebBrowser_V1

ffApp.Visible = True

Do While ffApp.Busy: DoEvents: Loop
Do Until ffApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set ffDoc = ffApp.document


ffApp.ExecWB OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT
ffApp.ExecWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT

Sheets("Sheet2").Select
With ActiveSheet
LastCol = .Cells(3, .Columns.Count).End(xlToLeft).Column
End With
h = LastCol + 1
Cells(1, h).Select
ActiveSheet.PasteSpecial Format:="Unicode Text", link:=False, _
DisplayAsIcon:=False
Sheets("Sheet1").Select

ffApp.Quit
Set ffApp = Nothing
End Sub'

1条回答
Explosion°爆炸
2楼-- · 2019-09-03 15:37

Windows machines:

Install selenium basic from here. Then add a reference to selenium type library via vbe > tools > references. FireFox driver .exe must be in a folder that is on the environmental path or many users find putting it in the selenium folder works. I don't think you can use the latest FireFox versions. I think you need FF v.46.0.1.

Option Explicit
Public Sub ScrapeWithFireFox()
    Dim d As WebDriver
    Set d = New FirefoxDriver
    Const URL = "https://stackoverflow.com/"

    With d
         .get URL
        'do something with page
        Stop '<==Delete me later
        .Quit
    End With
End Sub
查看更多
登录 后发表回答