この度GASに興味を持ち、独学で勉強を始めてみました。
試しにGoogleの検索結果画面をスクレイピングをしてみようと思い、まずは適当なワードの検索結果をクローリングしようと思ったのですが、以下のエラーでうまくいきませんでした。
Google検索結果以外のwebサイトをいくつか試したところ、どれも何回実行してもエラーにならなかったので、何故Googleの検索結果だけエラーになるのか不思議に思い質問しました。
本当に初歩的な質問だとは思いますが、もしご存知の方がいらっしゃいましたら是非ご回答の程よろしくお願いします。
実現したいこと
Googleの検索結果をスクレイピングし、スプレッドシートに出力する。
クローリングのソースコード
var url = "https://www.google.com/search?q=" + '検索ワード' //日本語の場合エンコードする + "&num=1"; //とりあえずクローリングしたいので最小値 var params = { "muteHttpExceptions" : true }; var html = UrlFetchApp.fetch(url, params).getContentText();
発生しているエラーメッセージ
Logger.log(html);の中身
html
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2<html> 3<head><meta http-equiv="content-type" content="text/html; charset=utf-8"><meta name="viewport" content="initial-scale=1"><title>https://www.google.com/search?q=%E3%82%B9%E3%82%AF%E3%83%AC%E3%82%A4%E3%83%94%E3%83%B3%E3%82%B0&num=3</title></head> 4<body style="font-family: arial, sans-serif; background-color: #fff; color: #000; padding:20px; font-size:18px;" onload="e=document.getElementById('captcha');if(e){e.focus();}"> 5<div style="max-width:400px;"> 6<hr noshade size="1" style="color:#ccc; background-color:#ccc;"><br> 7<form id="captcha-form" action="index" method="post"> 8<script src="https://www.google.com/recaptcha/api.js" async defer></script> 9<script>var submitCallback = function(response) {document.getElementById('captcha-form').submit();};</script> 10<div id="recaptcha" class="g-recaptcha" data-sitekey="6LfwuyUTAAAAAOAmoS0fdqijC2PbbdH4kjq62Y1b" data-callback="submitCallback" data-s="acUSMAjZwYF373nWEUoAUzRMnpi5pq9mF0oMKQQf36qCqWo4mDp-UsCQamvsIMout9Fk4EVR-Tsd77BSXjVqmii6IeEqSIUtKfl7tSWdshpdGsiXaCQv-Cajxh1rPn1LeS5STCkZ44efRv-YahWTqN17pJRq6_iJ80uEqrA18XhPgrakv38nlq0QyPahUwX6g2EHvv7PKLWh3iFr-go48JrTynQiAk1PMFrdg2O5OVzpsidmJRmvT3w"></div> 11<input type='hidden' name='q' value='EgQju4QHGMygiugFIhkA8aeDS-3-zQszxVZAfJzPa0drktHXnPepMgFy'><input type="hidden" name="continue" value="https://www.google.com/search?q=%E3%82%B9%E3%82%AF%E3%83%AC%E3%82%A4%E3%83%94%E3%83%B3%E3%82%B0&num=3"> 12</form> 13<hr noshade size="1" style="color:#ccc; background-color:#ccc;"> 14 15<div style="font-size:13px;"> 16<b>About this page</b><br><br>Our systems have detected unusual traffic from your computer network. This page checks to see if it's really you sending the requests, and not a robot. <a href="#" onclick="document.getElementById('infoDiv').style.display='block';">Why did this happen?</a><br><br> 17<div id="infoDiv" style="display:none; background-color:#eee; padding:10px; margin:0 0 15px 0; line-height:1.4em;"> 18This page appears when Google automatically detects requests coming from your computer network which appear to be in violation of the <a href="//www.google.com/policies/terms/">Terms of Service</a>. The block will expire shortly after those requests stop. In the meantime, solving the above CAPTCHA will let you continue to use our services.<br><br>This traffic may have been sent by malicious software, a browser plug-in, or a script that sends automated requests. If you share your network connection, ask your administrator for help — a different computer using the same IP address may be responsible. <a href="//support.google.com/websearch/answer/86640">Learn more</a><br><br>Sometimes you may be asked to solve the CAPTCHA if you are using advanced terms that robots are known to use, or sending requests very quickly. 19</div> 20 21IP address: xxx.xxx.xxx.xxx<br>Time: 2019-06-13T18:05:00Z<br>URL: https://www.google.com/search?q=%E3%82%B9%E3%82%AF%E3%83%AC%E3%82%A4%E3%83%94%E3%83%B3%E3%82%B0&num=3<br> 22</div> 23</div> 24</body> 25</html>
メッセージを読む限りだと、ロボットではないことを証明しろという、よくある画像選択テストの画面が返ってきているのかと予想しています。
また、他サイトではparamsを設定しなくても問題ないが、Google検索結果の場合muteHttpExceptionsを設定しないと、以下のエラーメッセージが出てくる。
https://www.google.com/search?q=%E3%82%B9%E3%82%AF%E3%83%AC%E3%82%A4%E3%83%94%E3%83%B3%E3%82%B0&num=1 のリクエストに失敗しました(エラー: 429)。サーバー応答の一部: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head><meta http-equiv="content-type" content="text/html; charset=utf-8"><me...(応答の全文を見るには muteHttpExceptions オプションを使用してください)(行 181、ファイル「test」)

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/13 22:22
2019/06/14 02:10
2019/06/14 03:34