• Home
  • Market
  • Finance
  • Running
  • Reading
  • Research
  • Travel
  • Lifestyle
  • About Me
No Result
View All Result
  • Home
  • Market
  • Finance
  • Running
  • Reading
  • Research
  • Travel
  • Lifestyle
  • About Me
No Result
View All Result
閱讀、跑步與書寫 ~~只要努力學習,知識一定能夠成為力量

自學VBA系列,網頁抓取的按鈕選擇及下拉式選單問題

2022 年 12 月 29 日
Home Reading Programming

網頁抓取的過程中經常會出現各種各樣的問題,今天就按鈕選擇及下拉式選單碰到的問題略作解釋。

按鈕選擇問題:

Sub NewUpdatedata() ‘
Dim ur As String, b, ie As Object


ur = "http://abc.com/"

Set ie = CreateObject("internetexplorer.application") '使用此方式可以免除 "設定引用項目"

With ie
    .Visible = False 'True為開啟ie, False為不開啟ie
    .Navigate ur
    Do While .ReadyState <> 4 '等待網頁開啟
        DoEvents
    Loop

Application.Wait (Now + TimeValue("0:00:8"))

一般的抓網頁程式碼,set ie object,loop之後要加個等待時間,有時轉換頁面或按鈕會牽涉到龐大的資料量,

需要有足夠的靜止時間,

.Document.getElementByID(“XXXX”).Click

再查找相對應的按鍵的名稱,如果找到ID就更方便,在之前的with裡面接著輸入.Click,

等待時間加上.Click應該可以解決問題。

下拉式選單(dropdown menu)問題:

反而下拉式選單更麻煩一點,不像前面的click就改變頁面,出現需要的資訊。用debug.print看過,明明裡面的值改變了,明明選項的值改變了,但是相對應的資訊卻沒有跑出來,類似的感覺就是你選了頁面,卻沒有按確定鍵。

這個時候我們需要再宣告一個object b進行操作。因為沒有特定id只好從ClassName著手,在第0個位置找到目標,

Set b = .Document.getElementsByClassName(“form-control font-weight-bold”)(0) ‘下拉式選單

Dim event_onChange As Object
Set event_onChange = .Document.createEvent(“HTMLEvents”)
event_onChange.initEvent “change”, True, False

b.selectedIndex = 1
b.FireEvent “onchange”
b.dispatchEvent event_onChange

然後再下面再宣告一個叫onChange的object進行event操作,

b.selectedIndex為不同下拉選項的值,例如”a,1,2,m”等等,需要的資訊應該會跑出來。

難題解決:

最後再按一貫的方法把Hyperlink裡面的值丟到Excel的儲存格裡面,至此,應該可以解決按鈕或者下拉式選單最常碰到的問題。

Set a = .Document.getElementsByTagname(“td”)

 y = 1

 For Each Hyperlink In a


 Cells(y, 1) = Hyperlink.INNERTEXT

   If Cells(y, 1) <> 0 Then
     y = y + 1
       End If
 Next
 End With
    
    ie.Quit
   
End Sub
Tags: ExcelVBA程式學習自學
Share196Tweet122Share49
Charlie chacha

Charlie chacha

Related Posts

持續買進
Finance

關於投資,讀《持續買進》的心得

2024 年 4 月 24 日

今天我們來分享一下最近非常熱門的財經書籍《持續買進》,怎麼說呢?整本書讀下來,也是有所收獲,可是,我個人覺得沒有外界所說那麼高度評價。 其實這本書所說的策略或建議也不是什麼新鮮的東西,只是作者用了一個...

EXCEL VLOOKUP 文字轉數字 數字轉文字
Programming

自學Excel系列 – 使用Vlookup函數的1個小技巧, 文字與數字之間的轉換問題(文字轉數字 數字轉文字)

2023 年 7 月 6 日

Vlookup函數 Vlookup的泛用性相信學過Excel的人都應該知道,有時候因為資料的性質,我們無法(最主要是沒有時間)一格一格去改變資料的屬性,導致我們使用Vlookup時無法找到目標的值,所...

自學Excel VBA系列-如何用VBA控制OutLook?
Lifestyle

Excel強大的組合函數練習,資料庫關鍵字搜索,Address + Match + Index + Find 組合拳

2023 年 3 月 18 日

想要把Excel的功能變得強大,函數組合使用必不可少,例如之前介紹的Offset函數,《一個強大的Excel函數OFFSET,動態選取資料範圍》,非常好用。本文介紹另外一個經常會使用到的組合拳:Add...

AI人工智能
Market

探索人工智能,了解OpenAI的未來與潛力–ChatGPT使用心得 ChatGPT可以幫到你做什麼?人工智能 語言生成 數據分析

2023 年 7 月 6 日

OpenAI ChatGPT ChatGPT的名號,大家都應該非常熟悉。地區破解什麼就不討論,兩個條件,地區VPN,以及SMS認證服務,還不能是虛擬號碼,我花了幾天時間也找不到方便安全的省錢方法,建議...

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

About Me

閱讀、跑步與書寫 ~~只要努力學習,知識一定能夠成為力量

Charlie Chacha

Blogger and Runner

Welcome to my blog! I'm Charlie Chacha, and here I'll be sharing valuable insights on living a fulfilling life and achieving financial success. Join me as we delve into topics such as effective time management, personal growth, and the art of learning. I firmly believe that putting in the effort and embracing continuous learning is the key to unlocking a prosperous future. So, let's embark on this journey together and discover how to thrive in both life and finances!

Categories

  • Finance (37)
  • Lifestyle (7)
  • Market (22)
  • Programming (13)
  • Reading (41)
  • Research (21)
  • Running (41)
  • Sports (2)
  • Travel (3)
  • Uncategorized (1)

Popular

  • JAVA學習筆記 ArrayList

    一個強大的Excel函數OFFSET,動態選取資料範圍

    2401 shares
    Share 960 Tweet 600
  • Excel VBA的自學心得分享

    1915 shares
    Share 766 Tweet 479
  • 自學Excel VBA系列-如何用VBA控制WORD?

    1430 shares
    Share 572 Tweet 358
  • 自學Excel VBA系列-如何用VBA控制OutLook?

    1189 shares
    Share 476 Tweet 297
  • 自學Excel系列 – 使用Vlookup函數的1個小技巧, 文字與數字之間的轉換問題(文字轉數字 數字轉文字)

    621 shares
    Share 248 Tweet 155

Instagram

    Go to the Customizer > JNews : Social, Like & View > Instagram Feed Setting, to connect your Instagram account.
No Result
View All Result
  • Home
  • Market
  • Finance
  • Running
  • Reading
  • Research
  • Travel
  • Lifestyle
  • About Me