2025/03/21

Py / VBA / 使用selenium爬蟲-更新程式碼

爬蟲跟反爬蟲是競爭的狀態

道高一尺魔高一丈

所以爬蟲的時候都有可能會遇到新的問題

之前在 VBA / 使用Excel VBA爬蟲Google搜尋結果的程式碼

會被google搜尋擋下來



由於網路上的selenium basic的資料不多,搜尋到的多半是python的版本

所以改用python的selenium-webdriver來搜尋資料,先修改python的程式碼

在這篇 How to Avoid Bot Detection With Selenium 的第2個方法

2. Disabling the Automation Indicator WebDriver Flags

就可以避免被阻擋

所以我嘗試用這樣的思路來設定VBA selenium basic

經過測試之後,只要多加這個參數就可以了

"--disable-blink-features=AutomationControlled"

其他用來隱藏"Chrome目前受到自動測試軟體控制"的設定都沒效果、或者用了反而會被阻擋



修改後的程式碼如下,主要是多了第5行的程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


Public BOT As New WebDriver

Sub WebDriver()
s = Application.EncodeURL("壯圍國中")
BOT.AddArgument "--disable-blink-features=AutomationControlled"
BOT.Start "chrome"
BOT.Get "https://www.google.com/search?q=" + s

s = BOT.FindElementsByClass("LrzXr")(1).Text

Debug.Print s

Application.Wait (Now + TimeValue("00:00:03"))

BOT.Quit

End Sub





 

0 comments:

張貼留言