vba爬动态网页

28 篇文章 2 订阅

原贴地址:https://blog.csdn.net/u011410413/article/details/54629770
vba执行js:http://club.excelhome.net/thread-1303169-1-1.html
代码会打开IE浏览器逐步操作,和使用python selenium的效果一样。没有找到更换浏览器的方法。
‘’'my test is over here以下的代码全部未测试

Sub aa()
    url = "http://club.excelhome.net/thread-1466658-1-1.html?tdsourcetag=s_pcqq_aiomsg"
    Call WebCrawler(url, Empty)
End Sub

Sub WebCrawler(ByVal GUrl, ByRef DraftPage)

	Dim sKey As String
	Dim k As Integer

    sKey = "Time In Source Status"
    k = 0


	Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")

    With IE

            .Visible = True
            .navigate GUrl
            Do Until .readystate = 4  'Complete
                    DoEvents
            Loop
            
            Set ele = .document.querySelector(".fastlg_l button em")
		    Set oWindow = .document.parentWindow
		    '''can execute javascript code,alert will block,after click ok vba code goes on
		    oWindow.execScript "var t=3;alert(t);"              
		    t = oWindow.t
            'ele.Value = "fa xin tie value"
            ele.innerhtml = "fa xin tie innerhtml"
            ele.Click			
            IE.Quit				'''my test is over here
            If (.document.querySelectorAll("div.aui-group aui-group-split").Length > 0) Then

                    Do Until (.document.querySelectorAll("div.wrap").Length > 0)
                        WaitToIEReady IE
                    Loop
                    GoTo Content
            End If

            On Error GoTo Content

            .document.getElementById("login-form-username").Value = "yiyi"
            .document.getElementById("login-form-password").Value = "yaya"
            .document.getElementById("login-form-submit").Click

            Do Until .readystate = 4  'Complete
                    DoEvents
            Loop
            Do Until (.document.querySelectorAll("div.wrap").Length > 0)
                    WaitToIEReady IE
            Loop

Content:
            .document.getElementById("all-tabpanel").Click
            Do Until (.document.querySelectorAll("div.actionContainer").Length > 0)
                WaitToIEReady IE
            Loop


            Do While k < 1000:  'Favorite part
                Set tables = .document.getElementsBytagname("table")
                k = k + 1
                For Each tabl In tables

                    For Each oRow In tabl.Rows
                        For Each oCell In oRow.Cells
                           If Trim(oCell.innertext) = sKey Then GoTo CopyWork
                        Next
                    Next
                Next
                If k = 999 Then MsgBox "cannot find page": Exit Sub
            Loop



CopyWork:
            'Don't forget to close IE Clipboard warning in IE Option->SecurityTab->Custom Level->Scripting session
            .document.execCommand "SelectAll"
            .document.execCommand "copy"

            'Paste all to put the webpage content
            DraftPage.Range("A1").Select
            Application.ActiveSheet.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False



      End With
      IE.Quit


End Sub

Sub WaitToIEReady(ByRef IeObj)
    Do While IeObj.Busy
        DoEvents
    Loop
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VBA是一种用于自动化操作Microsoft Office软件的编程语言,可以通过编写VBA代码实现各种任务。在使用VBA虫下载*.xls文件时,可以按照以下步骤进行: 1. 打开Excel软件,按下Alt+F11键,打开VBA编辑器。 2. 在VBA编辑器中,插入一个新的模块,可以通过点击"插入"菜单,然后选择"模块"。 3. 在新创建的模块中,编写VBA代码实现虫功能。以下是一个简单示例: ``` Sub DownloadXlsFile() Dim url As String Dim savePath As String ' 设置要下载的*.xls文件的URL地址 url = "http://example.com/example.xls" ' 设置保存文件的路径和名称 savePath = "C:\example.xls" ' 创建一个新的InternetExplorer对象 Dim ie As Object Set ie = CreateObject("InternetExplorer.Application") ' 使IE窗口对用户可见 ie.Visible = True ' 打开指定的URL ie.Navigate url ' 等待IE窗口加载页面完成 Do While ie.Busy Or ie.readyState <> 4 DoEvents Loop ' 保存页面为指定路径的文件 ie.Document.SaveAs savePath ' 关闭IE窗口 ie.Quit ' 释放IE对象的内存 Set ie = Nothing MsgBox "下载完成!" End Sub ``` 4. 保存并关闭VBA编辑器。 5. 返回Excel界面,按下Alt+F8键,打开宏对话框。 6. 在宏对话框中,选中刚才创建的宏"DownloadXlsFile",然后点击"运行"按钮执行宏。 7. VBA代码将自动打开一个IE窗口,加载指定的URL,然后将页面保存为指定路径的*.xls文件。 请注意,这只是一个简单的示例,具体的代码和操作会根据不同的需求和站而有所不同。在实际应用中,还需要考虑络请求的处理、页面元素的定位等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值