案例说明

       通过UiBot Creator抓取社区帖子数据(https://forum.uibot.com.cn/), 存储到Excel中

效果展示


案例实现

    /*
    功能: 社区帖子数据抓取
    作者: By Hejing
    时间: 2020/8/19

    注意点: 可以直接运行
    */
    // 1, 定义变量
    dim objExcelWorkBook = ""
    dim arrayData = ""
    dim iRet = ""
    dim hWeb = ""

    // 2,打开,绑定浏览器
    Try
        hWeb = WebBrowser.BindBrowser("chrome",10000,{"bContinueOnError":false,"iDelayAfter":300,"iDelayBefore":200})
    iRet = WebBrowser.GoURL(hWeb,"https://forum.uibot.com.cn/",true,"",30000,{"bContinueOnError":false,"iDelayAfter":300,"iDelayBefore":200})
    Catch e
        hWeb = WebBrowser.Create("chrome","https://forum.uibot.com.cn/",30000,{"bContinueOnError":false,"iDelayAfter":300,"iDelayBefore":200,"sBrowserPath":"","sStartArgs":""})
    End Try

    // 3, 数据抓取
    arrayData = UiElement.DataScrap({"html":[{"tag":"MAIN"}],"wnd":[{"app":"chrome","cls":"Chrome_WidgetWin_1","title":"*"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}]},{"Columns":[{"props":["text","url"],"selecors":[{"className":"container","index":0,"prefix":"","tag":"div","value":"div.container"},{"className":"row","index":0,"prefix":">","tag":"div","value":"div.row"},{"className":"col-lg-9 main","index":0,"prefix":">","tag":"div","value":"div.col-lg-9.main"},{"className":"card card-threadlist","index":0,"prefix":">","tag":"div","value":"div.card.card-threadlist"},{"className":"card-body","index":0,"prefix":">","tag":"div","value":"div.card-body"},{"className":"list-unstyled threadlist mb-0","index":0,"prefix":">","tag":"ul","value":"ul.list-unstyled.threadlist.mb-0"},{"index":0,"prefix":">","tag":"li","value":"li"},{"className":"media-body","index":0,"prefix":">","tag":"div","value":"div.media-body"},{"className":"subject break-all","index":0,"prefix":">","tag":"div","value":"div.subject.break-all"},{"className":"","index":0,"prefix":">","tag":"a","value":"a"}]},{"props":["text"],"selecors":[{"className":"container","index":0,"prefix":"","tag":"div","value":"div.container"},{"className":"row","index":0,"prefix":">","tag":"div","value":"div.row"},{"className":"col-lg-9 main","index":0,"prefix":">","tag":"div","value":"div.col-lg-9.main"},{"className":"card card-threadlist","index":0,"prefix":">","tag":"div","value":"div.card.card-threadlist"},{"className":"card-body","index":0,"prefix":">","tag":"div","value":"div.card-body"},{"className":"list-unstyled threadlist mb-0","index":0,"prefix":">","tag":"ul","value":"ul.list-unstyled.threadlist.mb-0"},{"index":0,"prefix":">","tag":"li","value":"li"},{"className":"media-body","index":0,"prefix":">","tag":"div","value":"div.media-body"},{"className":"d-flex justify-content-between small mt-1","index":0,"prefix":">","tag":"div","value":"div.d-flex.justify-content-between.small.mt-1"},{"className":"","index":1,"prefix":">","tag":"div","value":"div:nth-child(1)"},{"className":"","index":3,"prefix":">","tag":"span","value":"span:nth-child(3)"},{"className":"username text-grey mr-1","index":0,"prefix":">","tag":"span","value":"span.username.text-grey.mr-1"}]}],"ExtractTable":0},{"objNextLinkElement":{"html":[{"aaname":"▶","parentid":"body","tag":"A"}],"wnd":[{"app":"chrome","cls":"Chrome_WidgetWin_1","title":"*"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}]},"iMaxNumberOfPage":3,"iMaxNumberOfResult":-1,"iDelayBetweenMS":1000,"bContinueOnError":false})
    TracePrint(arrayData)

    // 4, 打开,绑定Excel
    try 
    objExcelWorkBook = Excel.BindBook("帖子数据.xlsx")
    catch 
    objExcelWorkBook = Excel.OpenExcel(@res"帖子数据.xlsx",true)
    end try

    // 5, 写入数据
    Excel.WriteRow(objExcelWorkBook,"Sheet1","A1",["标题","链接","作者"],false)    
    For Each index,item In arrayData
        Excel.WriteRow(objExcelWorkBook,"Sheet1","A"&(index+2),item,false)    
    Next

    // 6,保存退出
    Excel.CloseExcel(objExcelWorkBook,true)

results matching ""

    No results matching ""