Google検索など、IPアドレスやCookieから関連付けられて結果が表示されるようなサイトで、正しい検索順位を取得したいため「Tor + Chrome + SeleniumBasic」を使ってスクレイピングをします。
Tor本体をダウンロード
以下のURLからTor本体をダウンロードしてきます。
32bit版
https://dist.torproject.org/torbrowser/11.0.11/tor-win32-0.4.6.10.zip
64bit版
https://dist.torproject.org/torbrowser/11.0.11/tor-win64-0.4.6.10.zip
適当なディレクトリに解凍して tor.exe を実行しておけばOKです。
Torネットワークが起動すると Socks5 127.0.0.1:9050 にてアクセスができます。

ChromeにてTor経由でWebにアクセスする
ChromeDriverの起動時にオプションにSocks5を使うように1行追加します。
(proxy-serverの行)
下記のサンプルでは、プライベートブラウジングモードも指定しています。
(incognitoの行)
Option Explicit Sub main() With New ChromeDriver .AddArgument "--proxy-server=socks5://localhost:9050" .AddArgument "--incognito" .Get "http://taruo.net/e/" MsgBox "!" End With End Sub

Tor経由でGoogle検索を行う
以下のコードを実行
Option Explicit Sub main() Dim driver As New ChromeDriver With driver .AddArgument "--proxy-server=socks5://localhost:9050" .AddArgument "--incognito" .Get "https://google.co.jp/" ClickElementByTagText driver, "div", "同意する" .Wait 300 .FindElementByName("q").Clear .FindElementByName("q").SendKeys "SeleniumBasic" .Wait 1000 .FindElementByTag("form").Submit MsgBox "ロボットチェックを手動でクリアしたらOKをクリック" For Each elmLoop In .FindElementsByTag("A") If elmLoop.FindElementsByTag("H3").Count Then Debug.Print elmLoop.FindElementByTag("H3").Text Debug.Print , elmLoop.Attribute("HREF") Debug.Print "--------------------------------------------------" End If Next End With End Sub Sub ClickElementByTagText(obj As Object, tag As String, txt As String) Dim elmLoop As WebElement For Each elmLoop In obj.FindElementsByTag(tag) If elmLoop.Text = txt Then elmLoop.Click Exit For End If Next End Sub
実行結果
「私はロボットではありません」チェックに引っかかると思うので、手動でクリアさせます。

検索結果が表示されます。

Visual Basicのイミディエイトウィンドウに結果が表示されています。
SeleniumBasicをインストールしてExcel(VBA)からWeb ... https://lil.la/archives/3436 -------------------------------------------------- Seleniumbasic - GitHub Pages https://florentbr.github.io/SeleniumBasic/ -------------------------------------------------- Excel VBAでSeleniumBasicを使わずにスクレイピングする https://qiita.com/uezo/items/66e20b064ffd5f239b9a -------------------------------------------------- 非エンジニアにこそ伝えたいSeleniumBasic | ツナググループHC https://www.indival.co.jp/2018/02/07/5918/ -------------------------------------------------- ノート】WEBブラウザを自動操作する!Selenium Basicの ... https://hirachin.com/post-5019/ -------------------------------------------------- 【VBA】Seleniumリファレンス(逆引き)【まとめ】 https://nkmrdai.com/vba-selenium-reference/ -------------------------------------------------- EXCEL VBAでプログラミング SeleniumBasicを使ったWeb ... http://kensyou-d.info/selenium.html -------------------------------------------------- VBAのスクレイピングを簡単楽にしてくれるSelenium https://excel-ubara.com/excelvba4/EXCEL_VBA_401.html -------------------------------------------------- Excel+SeleniumBasicでChrome操作・スクレイピング https://take-web.com/task_help/msoffice/excel/seleniumbasicc/ -------------------------------------------------- Selemium Basic VBA用ドキュメントの日本語訳メモ https://curio-shiki.com/blog/vba/selemium-basic-vba-documentation --------------------------------------------------
コメント