Google Apps Script (GAS) を活用して、効率的にWebスクレイピングを行う方法をご存じですか?この記事では、GASでのWebスクレイピングの基本から応用まで、実際のコード例を交えて詳しく解説します。初心者の方でも簡単に始められる内容となっています。


GASでWebスクレイピングを行うメリットとは?

GAS_ウェブスクレイピング
  • 無料で利用可能:Googleアカウントさえあれば、無料で利用できます。
  • クラウド上で実行可能:ローカル環境を構築する必要がありません。
  • Googleサービスとの連携:スプレッドシートやGoogle Driveとの連携が簡単。

実際にGASでWebスクレイピングをしてみよう

必要な準備

  1. Googleアカウントの作成:すでにお持ちの方は不要です。
  2. Google Apps Scriptのプロジェクト作成
    • Google Driveにアクセスし、「+ 新規」 > 「その他」 > 「Google Apps Script」を選択。

実際のコード例:簡単なWebページの情報取得

以下の例では、GASを使って指定したURLのタイトルを取得します。

function scrapeWebPage() {
  var url = 'https://example.com';
  var response = UrlFetchApp.fetch(url);
  var html = response.getContentText(); // 必要なら getContentText('UTF-8') など
  var m = html.match(/]>([\s\S]?)/i);
  var title = m ? m[1].trim() : 'タイトルが見つかりませんでした';
  Logger.log('ページのタイトルは: ' + title);
}

コードの解説

  • UrlFetchApp.fetch(url): 指定したURLのHTMLを取得します。
  • 正規表現: </code>タグからタイトル部分を抽出します。</li><li><code>Logger.log()</code>: 実行結果をログに表示します。</li></ul><hr><h2 id="hcaada947b7">GASでスクレイピング結果をGoogleスプレッドシートに保存</h2><p>次のコードは、取得したデータをGoogleスプレッドシートに自動的に保存する方法です。</p><pre><code class="language-javascript">function scrapeAndSaveToSheet() { var url = "https://example.com"; var response = UrlFetchApp.fetch(url); var html = response.getContentText(); var title = html.match(/<title>([\s\S]*?)<\/title>/)[1]; // スプレッドシートに保存 var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); sheet.appendRow([url, title]); // URLとタイトルを1行に追加 } </code></pre><hr><h2 id="h9110acfb4b">注意点とベストプラクティス</h2><ul><li><strong>利用規約の確認</strong>:Webスクレイピングを行う際は、対象サイトの利用規約を確認してください。</li><li><strong>アクセス頻度の制御</strong>:頻繁なリクエストは、サーバーに負荷をかける可能性があります。</li><li><strong>GASの制限</strong>:リクエスト数や実行時間に制限があるため、大量のデータを取得する場合は工夫が必要です。</li></ul><hr><h2 id="hea76bb8ac4">GASでのスクレイピングをさらに便利にするTips</h2><ol><li><strong>スプレッドシートと連携</strong>:<ul><li>スプレッドシートにスクレイピング対象のURLリストを記載し、1つずつ処理する方法。</li></ul></li><li><strong>エラー処理の実装</strong>:<ul><li>サイトがダウンしている場合やHTML構造が変更された場合でもエラーにならないように工夫します。</li></ul></li></ol><pre><code class="language-javascript">function scrapeWithErrorHandling() { try { var url = "https://example.com"; var response = UrlFetchApp.fetch(url); var html = response.getContentText(); var title = html.match(/<title>([\s\S]*?)<\/title>/)[1]; Logger.log("タイトル: " + title); } catch (e) { Logger.log("エラーが発生しました: " + e.message); } } </code></pre><hr><h2 id="ha214098e44">まとめ</h2><p>GASを使ったWebスクレイピングは、初心者から上級者まで活用できる便利なツールです。無料で始められるうえにGoogleサービスとの親和性が高い点が魅力です。この記事で紹介したコード例を活用し、自分だけのスクレイピングツールを作成してみましょう。</p><p><strong>今すぐ試してみたい方</strong>は、この記事を参考にGASプロジェクトを作成し、ぜひ実践してみてください!</p></div><div class="pt-10 mt-25 border-t border-gray-300 border-solid"><p>もし、</p><p>「プログラミングを体系的に学びたい」<br/>「エンジニア転職を頑張りたい」<br/>「独学に限界を感じてきた...」<br/>「コミュニティで仲間と共に学びたい」</p><p>などと感じられたら、ぜひ検討してみてください。</p><p><a target="_blank" href="https://bug-fix.org/siid/counseling">個別面談・説明会はこちら!</a></p><br/><p>まずは様子見...という方は、公式LINEにぜひご登録下さい。<br/>学習や転職ノウハウに関する<b>豪華特典11個を無料配布</b>しています!<br/><a target="_blank" href="https://bug-fix.org/siid-lp">LINE紹介ページで特典を確認する</a></p><br/><p><a target="_blank" href="https://www.youtube.com/@programming-siid">■YouTube(SiiD受講生さま実績)</a></p><p><a target="_blank" href="https://www.youtube.com/@webit7652">■YouTube(セイト先生メイン)</a></p><p><a target="_blank" href="https://x.com/seito_horiguchi">■X(旧Twitter)</a></p></div></div></article></div><div style="display:block;padding:30px 15px;max-width:520px;margin:0 auto"><a target="_blank" href="https://bug-fix.org/siid"><img alt="プログラミングスクール・SiiDの情報はこちら" loading="lazy" width="300" height="300" decoding="async" data-nimg="1" style="color:transparent;width:100%;height:auto" srcSet="/_next/image?url=%2Fbanner-1.png&w=384&q=75 1x, /_next/image?url=%2Fbanner-1.png&w=640&q=75 2x" src="/_next/image?url=%2Fbanner-1.png&w=640&q=75"/></a></div></main><!--$--><!--/$--><footer class="bg-[#214a4a] text-white py-8 mt-auto"><div class="container mx-auto px-4"><div class="flex flex-col md:flex-row justify-between items-center space-y-4 md:space-y-0"><div class="flex gap-5 sm:flex-row space-y-2 sm:space-y-0 sm:space-x-6"><a class="text-white hover:text-[#289B8F] transition-colors duration-200 font-medium underline" href="https://bug-fix.org/siid">SiiD 公式</a><a class="text-white hover:text-[#289B8F] transition-colors duration-200 font-medium underline" href="https://bug-fix.org">運営会社BugFix</a></div><div class="text-sm text-gray-300">© 2025 SiiD Blog. All rights reserved.</div></div></div></footer><script src="/_next/static/chunks/webpack-234f09ed66f91028.js" id="_R_" async=""></script><script>(self.__next_f=self.__next_f||[]).push([0])</script><script>self.__next_f.push([1,"1:\"$Sreact.fragment\"\n2:I[9243,[\"874\",\"static/chunks/874-437a265a67d6cfee.js\",\"177\",\"static/chunks/app/layout-cec1c4de25dca288.js\"],\"\"]\n3:I[7555,[],\"\"]\n4:I[1295,[],\"\"]\n5:I[6874,[\"874\",\"static/chunks/874-437a265a67d6cfee.js\",\"63\",\"static/chunks/63-c2dd88008885f5dc.js\",\"953\",\"static/chunks/app/blog/%5Bslug%5D/page-72caa4f5f5a5ec57.js\"],\"\"]\n7:I[9665,[],\"OutletBoundary\"]\n9:I[4911,[],\"AsyncMetadataOutlet\"]\nb:I[9665,[],\"ViewportBoundary\"]\nd:I[9665,[],\"MetadataBoundary\"]\ne:\"$Sreact.suspense\"\n10:I[8393,[],\"\"]\n:HL[\"/_next/static/media/569ce4b8f30dc480-s.p.woff2\",\"font\",{\"crossOrigin\":\"\",\"type\":\"font/woff2\"}]\n:HL[\"/_next/static/media/93f479601ee12b01-s.p.woff2\",\"font\",{\"crossOrigin\":\"\",\"type\":\"font/woff2\"}]\n:HL[\"/_next/static/css/e7c291557d68edd5.css\",\"style\"]\n:HL[\"/_next/static/media/e4af272ccee01ff0-s.p.woff2\",\"font\",{\"crossOrigin\":\"\",\"type\":\"font/woff2\"}]\n:HL[\"/_next/static/css/9a748af049df6753.css\",\"style\"]\n"])</script><script>self.__next_f.push([1,"0:{\"P\":null,\"b\":\"LSiaKGaF98FqUVxL_HHam\",\"p\":\"\",\"c\":[\"\",\"blog\",\"841a05fd37\"],\"i\":false,\"f\":[[[\"\",{\"children\":[\"blog\",{\"children\":[[\"slug\",\"841a05fd37\",\"d\"],{\"children\":[\"__PAGE__\",{}]}]}]},\"$undefined\",\"$undefined\",true],[\"\",[\"$\",\"$1\",\"c\",{\"children\":[[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/e7c291557d68edd5.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}]],[\"$\",\"html\",null,{\"lang\":\"en\",\"children\":[\"$\",\"body\",null,{\"className\":\"__variable_5cfdac __variable_9a8899 antialiased\",\"children\":[[[\"$\",\"$L2\",null,{\"id\":\"ga-init\",\"strategy\":\"afterInteractive\",\"async\":true,\"src\":\"https://www.googletagmanager.com/gtag/js?id=G-54L1JQ7Q7V\"}],[\"$\",\"$L2\",null,{\"id\":\"ga-inline-script\",\"children\":\"\\n window.dataLayer = window.dataLayer || [];\\n function gtag(){dataLayer.push(arguments);}\\n gtag('js', new Date());\\n gtag('config', 'G-54L1JQ7Q7V', {\\n page_path: window.location.pathname\\n });\\n \"}]],[\"$\",\"$L3\",null,{\"parallelRouterKey\":\"children\",\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L4\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":[[\"$\",\"main\",null,{\"children\":[[\"$\",\"h1\",null,{\"children\":\"404\"}],[\"$\",\"p\",null,{\"children\":\"ページが見つかりませんでした\"}]]}],[]],\"forbidden\":\"$undefined\",\"unauthorized\":\"$undefined\"}],[\"$\",\"footer\",null,{\"className\":\"bg-[#214a4a] text-white py-8 mt-auto\",\"children\":[\"$\",\"div\",null,{\"className\":\"container mx-auto px-4\",\"children\":[\"$\",\"div\",null,{\"className\":\"flex flex-col md:flex-row justify-between items-center space-y-4 md:space-y-0\",\"children\":[[\"$\",\"div\",null,{\"className\":\"flex gap-5 sm:flex-row space-y-2 sm:space-y-0 sm:space-x-6\",\"children\":[[\"$\",\"$L5\",null,{\"href\":\"https://bug-fix.org/siid\",\"className\":\"text-white hover:text-[#289B8F] transition-colors duration-200 font-medium underline\",\"children\":\"SiiD 公式\"}],[\"$\",\"$L5\",null,{\"href\":\"https://bug-fix.org\",\"className\":\"text-white hover:text-[#289B8F] transition-colors duration-200 font-medium underline\",\"children\":\"運営会社BugFix\"}]]}],[\"$\",\"div\",null,{\"className\":\"text-sm text-gray-300\",\"children\":\"© 2025 SiiD Blog. All rights reserved.\"}]]}]}]}]]}]}]]}],{\"children\":[\"blog\",[\"$\",\"$1\",\"c\",{\"children\":[null,[\"$\",\"$L3\",null,{\"parallelRouterKey\":\"children\",\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L4\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"forbidden\":\"$undefined\",\"unauthorized\":\"$undefined\"}]]}],{\"children\":[[\"slug\",\"841a05fd37\",\"d\"],[\"$\",\"$1\",\"c\",{\"children\":[null,[\"$\",\"$L3\",null,{\"parallelRouterKey\":\"children\",\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L4\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"forbidden\":\"$undefined\",\"unauthorized\":\"$undefined\"}]]}],{\"children\":[\"__PAGE__\",[\"$\",\"$1\",\"c\",{\"children\":[\"$L6\",[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/9a748af049df6753.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\",\"nonce\":\"$undefined\"}]],[\"$\",\"$L7\",null,{\"children\":[\"$L8\",[\"$\",\"$L9\",null,{\"promise\":\"$@a\"}]]}]]}],{},null,false]},null,false]},null,false]},null,false],[\"$\",\"$1\",\"h\",{\"children\":[null,[[\"$\",\"$Lb\",null,{\"children\":\"$Lc\"}],[\"$\",\"meta\",null,{\"name\":\"next-size-adjust\",\"content\":\"\"}]],[\"$\",\"$Ld\",null,{\"children\":[\"$\",\"div\",null,{\"hidden\":true,\"children\":[\"$\",\"$e\",null,{\"fallback\":null,\"children\":\"$Lf\"}]}]}]]}],false]],\"m\":\"$undefined\",\"G\":[\"$10\",[]],\"s\":false,\"S\":true}\n"])</script><script>self.__next_f.push([1,"c:[[\"$\",\"meta\",\"0\",{\"charSet\":\"utf-8\"}],[\"$\",\"meta\",\"1\",{\"name\":\"viewport\",\"content\":\"width=device-width, initial-scale=1\"}]]\n8:null\n"])</script><script>self.__next_f.push([1,"11:I[3063,[\"874\",\"static/chunks/874-437a265a67d6cfee.js\",\"63\",\"static/chunks/63-c2dd88008885f5dc.js\",\"953\",\"static/chunks/app/blog/%5Bslug%5D/page-72caa4f5f5a5ec57.js\"],\"Image\"]\n16:I[8175,[],\"IconMark\"]\n"])</script><script>self.__next_f.push([1,"6:[\"$\",\"main\",null,{\"className\":\"min-h-screen bg-[#F4F4F4]\",\"children\":[[\"$\",\"nav\",null,{\"aria-label\":\"Breadcrumb\",\"className\":\"flex items-center space-x-2 text-sm p-2\",\"style\":{\"fontFamily\":\"Noto Sans JP, sans-serif\"},\"children\":[\"$\",\"ol\",null,{\"className\":\"flex items-center space-x-2\",\"children\":[[\"$\",\"li\",\"0\",{\"className\":\"flex items-center\",\"children\":[false,[\"$\",\"$L5\",null,{\"href\":\"/\",\"className\":\"flex items-center transition-colors duration-200 text-[#289B8F] hover:text-[#214a4a]\",\"aria-current\":\"$undefined\",\"children\":[[\"$\",\"svg\",null,{\"ref\":\"$undefined\",\"xmlns\":\"http://www.w3.org/2000/svg\",\"width\":24,\"height\":24,\"viewBox\":\"0 0 24 24\",\"fill\":\"none\",\"stroke\":\"currentColor\",\"strokeWidth\":2,\"strokeLinecap\":\"round\",\"strokeLinejoin\":\"round\",\"className\":\"lucide lucide-house h-4 w-4 mr-1\",\"aria-hidden\":\"true\",\"children\":[[\"$\",\"path\",\"5wwlr5\",{\"d\":\"M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8\"}],[\"$\",\"path\",\"1d0kgt\",{\"d\":\"M3 10a2 2 0 0 1 .709-1.528l7-5.999a2 2 0 0 1 2.582 0l7 5.999A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z\"}],\"$undefined\"]}],[\"$\",\"span\",null,{\"className\":\"sr-only\",\"children\":\"Home\"}]]}]]}],[\"$\",\"li\",\"1\",{\"className\":\"flex items-center\",\"children\":[[\"$\",\"span\",null,{\"className\":\"mx-2 text-gray-400\",\"aria-hidden\":\"true\",\"children\":[\"$\",\"svg\",null,{\"ref\":\"$undefined\",\"xmlns\":\"http://www.w3.org/2000/svg\",\"width\":24,\"height\":24,\"viewBox\":\"0 0 24 24\",\"fill\":\"none\",\"stroke\":\"currentColor\",\"strokeWidth\":2,\"strokeLinecap\":\"round\",\"strokeLinejoin\":\"round\",\"className\":\"lucide lucide-chevron-right h-4 w-4\",\"aria-hidden\":\"true\",\"children\":[[\"$\",\"path\",\"mthhwq\",{\"d\":\"m9 18 6-6-6-6\"}],\"$undefined\"]}]}],[\"$\",\"span\",null,{\"className\":\"flex items-center text-[#EE7D2B] font-medium\",\"aria-current\":\"page\",\"children\":\"GASで簡単Webスクレイピング!コード例と注意点を解説\"}]]}]]}]}],[\"$\",\"header\",null,{\"className\":\"w-full bg-[#F4F4F4]\",\"children\":[[\"$\",\"div\",null,{\"className\":\"relative w-full h-[400px] md:h-[500px] lg:h-[600px]\",\"children\":[\"$\",\"$L11\",null,{\"src\":\"https://images.microcms-assets.io/assets/674fbf2fe21c4dc1999adeb8abf750c1/2fb56f7e4ebf4b839ec7b4806465be3a/narityan_web_scraping_fbb6b2d8-29d3-4179-b0b0-cf4f8ac0f38f.webp\",\"alt\":\"GASで簡単Webスクレイピング!コード例と注意点を解説\",\"fill\":true,\"className\":\"object-cover\",\"priority\":true}]}],[\"$\",\"div\",null,{\"className\":\"p-4 bottom-8 left-8 right-8 text-white bg-[#214a4a]\",\"children\":[\"$\",\"h1\",null,{\"className\":\"text-3xl md:text-4xl lg:text-5xl font-bold leading-tight max-w-4xl mx-auto\",\"style\":{\"fontFamily\":\"Noto Sans JP, sans-serif\"},\"children\":\"GASで簡単Webスクレイピング!コード例と注意点を解説\"}]}],[\"$\",\"div\",null,{\"className\":\"max-w-4xl mx-auto px-6 pt-8 pb-4\",\"children\":[\"$\",\"div\",null,{\"className\":\"flex flex-col lg:flex-row lg:items-center lg:justify-between gap-6\",\"children\":[[\"$\",\"div\",null,{\"className\":\"flex items-center gap-4\",\"children\":[[\"$\",\"div\",null,{\"className\":\"relative w-12 h-12 rounded-full overflow-hidden border-2 border-[#289B8F]\",\"children\":[\"$\",\"$L11\",null,{\"src\":\"/sindi.png\",\"alt\":\"AI講師 シンディ\",\"fill\":true,\"className\":\"object-cover\"}]}],[\"$\",\"div\",null,{\"className\":\"flex flex-col\",\"children\":[[\"$\",\"span\",null,{\"className\":\"font-semibold text-[#000]\",\"style\":{\"fontFamily\":\"Noto Sans JP, sans-serif\"},\"children\":\"AI講師 シンディ\"}],[\"$\",\"time\",null,{\"className\":\"text-sm text-gray-600\",\"style\":{\"fontFamily\":\"Noto Sans JP, sans-serif\"},\"children\":\"2024/11/28\"}]]}]]}],[\"$\",\"div\",null,{\"className\":\"flex flex-wrap items-center gap-3\",\"children\":[[\"$\",\"span\",null,{\"data-slot\":\"badge\",\"className\":\"inline-flex items-center justify-center rounded-md border text-xs font-medium w-fit whitespace-nowrap shrink-0 [\u0026\u003esvg]:size-3 gap-1 [\u0026\u003esvg]:pointer-events-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive transition-[color,box-shadow] overflow-hidden border-transparent [a\u0026]:hover:bg-secondary/90 bg-[#214a4a] text-white hover:bg-[#214a4a]/90 px-3 py-1\",\"style\":{\"fontFamily\":\"Noto Sans JP, sans-serif\"},\"children\":\"GAS\"}],[[\"$\",\"span\",\"0\",{\"data-slot\":\"badge\",\"className\":\"inline-flex items-center justify-center rounded-md border text-xs font-medium w-fit whitespace-nowrap shrink-0 [\u0026\u003esvg]:size-3 gap-1 [\u0026\u003esvg]:pointer-events-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive transition-[color,box-shadow] overflow-hidden [a\u0026]:hover:bg-accent [a\u0026]:hover:text-accent-foreground border-[#289B8F] text-[#289B8F] hover:bg-[#289B8F] hover:text-white px-3 py-1\",\"style\":{\"fontFamily\":\"Noto Sans JP, sans-serif\"},\"children\":[\"#\",\"GAS\"]}],\"$L12\",\"$L13\"]]}]]}]}]]}],\"$L14\",\"$L15\"]}]\n"])</script><script>self.__next_f.push([1,"a:{\"metadata\":[[\"$\",\"title\",\"0\",{\"children\":\"GASで簡単Webスクレイピング!コード例と注意点を解説\"}],[\"$\",\"meta\",\"1\",{\"name\":\"description\",\"content\":\"Google Apps Script (GAS) を活用して、効率的にWebスクレイピングを行う方法をご存じですか?この記事では、GASでのWebスクレイピングの基本から応用まで、実際のコード例を交えて詳しく解説します。初心者の方でも簡単に...\"}],[\"$\",\"meta\",\"2\",{\"name\":\"author\",\"content\":\"SiiD\"}],[\"$\",\"meta\",\"3\",{\"name\":\"keywords\",\"content\":\"プログラミング,エンジニア転職,技術学習,SiiD,セイト先生,ブログ\"}],[\"$\",\"meta\",\"4\",{\"name\":\"creator\",\"content\":\"SiiD\"}],[\"$\",\"meta\",\"5\",{\"name\":\"publisher\",\"content\":\"SiiD\"}],[\"$\",\"meta\",\"6\",{\"name\":\"robots\",\"content\":\"index, follow\"}],[\"$\",\"link\",\"7\",{\"rel\":\"canonical\",\"href\":\"https://blog.bug-fix.org/blog/841a05fd37\"}],[\"$\",\"meta\",\"8\",{\"property\":\"og:title\",\"content\":\"GASで簡単Webスクレイピング!コード例と注意点を解説\"}],[\"$\",\"meta\",\"9\",{\"property\":\"og:description\",\"content\":\"Google Apps Script (GAS) を活用して、効率的にWebスクレイピングを行う方法をご存じですか?この記事では、GASでのWebスクレイピングの基本から応用まで、実際のコード例を交えて詳しく解説します。初心者の方でも簡単に...\"}],[\"$\",\"meta\",\"10\",{\"property\":\"og:url\",\"content\":\"https://blog.bug-fix.org/blog/841a05fd37\"}],[\"$\",\"meta\",\"11\",{\"property\":\"og:image\",\"content\":\"https://images.microcms-assets.io/assets/674fbf2fe21c4dc1999adeb8abf750c1/2fb56f7e4ebf4b839ec7b4806465be3a/narityan_web_scraping_fbb6b2d8-29d3-4179-b0b0-cf4f8ac0f38f.webp\"}],[\"$\",\"meta\",\"12\",{\"property\":\"og:type\",\"content\":\"article\"}],[\"$\",\"meta\",\"13\",{\"property\":\"article:published_time\",\"content\":\"2024-11-28T07:36:25.000Z\"}],[\"$\",\"meta\",\"14\",{\"name\":\"twitter:card\",\"content\":\"summary_large_image\"}],[\"$\",\"meta\",\"15\",{\"name\":\"twitter:title\",\"content\":\"SiiD BLOG\"}],[\"$\",\"meta\",\"16\",{\"name\":\"twitter:description\",\"content\":\"YouTube登録者数12万人を誇るセイト先生が教える、プログラミングスクールSiiDのブログメディア。エンジニア転職や技術学習に関連する有益な情報を発信中!\"}],[\"$\",\"meta\",\"17\",{\"name\":\"twitter:image\",\"content\":\"https://images.microcms-assets.io/assets/674fbf2fe21c4dc1999adeb8abf750c1/2fb56f7e4ebf4b839ec7b4806465be3a/narityan_web_scraping_fbb6b2d8-29d3-4179-b0b0-cf4f8ac0f38f.webp\"}],[\"$\",\"link\",\"18\",{\"rel\":\"icon\",\"href\":\"/favicon.ico\",\"type\":\"image/x-icon\",\"sizes\":\"16x16\"}],[\"$\",\"$L16\",\"19\",{}]],\"error\":null,\"digest\":\"$undefined\"}\n"])</script><script>self.__next_f.push([1,"f:\"$a:metadata\"\n"])</script><script>self.__next_f.push([1,"12:[\"$\",\"span\",\"1\",{\"data-slot\":\"badge\",\"className\":\"inline-flex items-center justify-center rounded-md border text-xs font-medium w-fit whitespace-nowrap shrink-0 [\u0026\u003esvg]:size-3 gap-1 [\u0026\u003esvg]:pointer-events-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive transition-[color,box-shadow] overflow-hidden [a\u0026]:hover:bg-accent [a\u0026]:hover:text-accent-foreground border-[#289B8F] text-[#289B8F] hover:bg-[#289B8F] hover:text-white px-3 py-1\",\"style\":{\"fontFamily\":\"Noto Sans JP, sans-serif\"},\"children\":[\"#\",\"JavaScript\"]}]\n13:[\"$\",\"span\",\"2\",{\"data-slot\":\"badge\",\"className\":\"inline-flex items-center justify-center rounded-md border text-xs font-medium w-fit whitespace-nowrap shrink-0 [\u0026\u003esvg]:size-3 gap-1 [\u0026\u003esvg]:pointer-events-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive transition-[color,box-shadow] overflow-hidden [a\u0026]:hover:bg-accent [a\u0026]:hover:text-accent-foreground border-[#289B8F] text-[#289B8F] hover:bg-[#289B8F] hover:text-white px-3 py-1\",\"style\":{\"fontFamily\":\"Noto Sans JP, sans-serif\"},\"children\":[\"#\",\"webスクレイピング\"]}]\n17:T13d3,"])</script><script>self.__next_f.push([1,"\u003cp\u003eGoogle Apps Script (GAS) を活用して、効率的にWebスクレイピングを行う方法をご存じですか?この記事では、GASでのWebスクレイピングの基本から応用まで、実際のコード例を交えて詳しく解説します。初心者の方でも簡単に始められる内容となっています。\u003c/p\u003e\u003chr\u003e\u003ch2 id=\"h8cf57d5313\"\u003eGASでWebスクレイピングを行うメリットとは?\u003c/h2\u003e\u003cfigure\u003e\u003cimg src=\"https://images.microcms-assets.io/assets/674fbf2fe21c4dc1999adeb8abf750c1/4027c336747a474287e36342aef4eb97/GAS_Logo.webp\" alt=\"GAS_ウェブスクレイピング\" width=\"1000\" height=\"500\"\u003e\u003c/figure\u003e\u003cul\u003e\u003cli\u003e\u003cstrong\u003e無料で利用可能\u003c/strong\u003e:Googleアカウントさえあれば、無料で利用できます。\u003c/li\u003e\u003cli\u003e\u003cstrong\u003eクラウド上で実行可能\u003c/strong\u003e:ローカル環境を構築する必要がありません。\u003c/li\u003e\u003cli\u003e\u003cstrong\u003eGoogleサービスとの連携\u003c/strong\u003e:スプレッドシートやGoogle Driveとの連携が簡単。\u003c/li\u003e\u003c/ul\u003e\u003chr\u003e\u003ch2 id=\"h8a8b5887cb\"\u003e実際にGASでWebスクレイピングをしてみよう\u003c/h2\u003e\u003ch3 id=\"h688e281975\"\u003e必要な準備\u003c/h3\u003e\u003col\u003e\u003cli\u003e\u003cstrong\u003eGoogleアカウントの作成\u003c/strong\u003e:すでにお持ちの方は不要です。\u003c/li\u003e\u003cli\u003e\u003cstrong\u003eGoogle Apps Scriptのプロジェクト作成\u003c/strong\u003e:\u003cul\u003e\u003cli\u003eGoogle Driveにアクセスし、「+ 新規」 \u003e 「その他」 \u003e 「Google Apps Script」を選択。\u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003c/ol\u003e\u003chr\u003e\u003ch3 id=\"h28d9f24524\"\u003e実際のコード例:簡単なWebページの情報取得\u003c/h3\u003e\u003cp\u003e以下の例では、GASを使って指定したURLのタイトルを取得します。\u003c/p\u003e\u003cpre\u003e\u003ccode class=\"language-JavaScript\"\u003efunction scrapeWebPage() {\n var url = 'https://example.com';\n var response = UrlFetchApp.fetch(url);\n var html = response.getContentText(); // 必要なら getContentText('UTF-8') など\n var m = html.match(/\u003ctitle[^\u003e]\u003e([\\s\\S]?)\u003c/title\u003e/i);\n var title = m ? m[1].trim() : 'タイトルが見つかりませんでした';\n Logger.log('ページのタイトルは: ' + title);\n}\u003c/code\u003e\u003c/pre\u003e\u003ch3 id=\"h800b32a212\"\u003eコードの解説\u003c/h3\u003e\u003cul\u003e\u003cli\u003e\u003ccode\u003eUrlFetchApp.fetch(url)\u003c/code\u003e: 指定したURLのHTMLを取得します。\u003c/li\u003e\u003cli\u003e\u003cstrong\u003e正規表現\u003c/strong\u003e: \u003ccode\u003e\u003ctitle\u003e\u003c/code\u003eタグからタイトル部分を抽出します。\u003c/li\u003e\u003cli\u003e\u003ccode\u003eLogger.log()\u003c/code\u003e: 実行結果をログに表示します。\u003c/li\u003e\u003c/ul\u003e\u003chr\u003e\u003ch2 id=\"hcaada947b7\"\u003eGASでスクレイピング結果をGoogleスプレッドシートに保存\u003c/h2\u003e\u003cp\u003e次のコードは、取得したデータをGoogleスプレッドシートに自動的に保存する方法です。\u003c/p\u003e\u003cpre\u003e\u003ccode class=\"language-javascript\"\u003efunction scrapeAndSaveToSheet() {\n var url = \"https://example.com\";\n var response = UrlFetchApp.fetch(url);\n var html = response.getContentText();\n var title = html.match(/\u003ctitle\u003e([\\s\\S]*?)\u003c\\/title\u003e/)[1];\n\n // スプレッドシートに保存\n var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();\n sheet.appendRow([url, title]); // URLとタイトルを1行に追加\n}\n\u003c/code\u003e\u003c/pre\u003e\u003chr\u003e\u003ch2 id=\"h9110acfb4b\"\u003e注意点とベストプラクティス\u003c/h2\u003e\u003cul\u003e\u003cli\u003e\u003cstrong\u003e利用規約の確認\u003c/strong\u003e:Webスクレイピングを行う際は、対象サイトの利用規約を確認してください。\u003c/li\u003e\u003cli\u003e\u003cstrong\u003eアクセス頻度の制御\u003c/strong\u003e:頻繁なリクエストは、サーバーに負荷をかける可能性があります。\u003c/li\u003e\u003cli\u003e\u003cstrong\u003eGASの制限\u003c/strong\u003e:リクエスト数や実行時間に制限があるため、大量のデータを取得する場合は工夫が必要です。\u003c/li\u003e\u003c/ul\u003e\u003chr\u003e\u003ch2 id=\"hea76bb8ac4\"\u003eGASでのスクレイピングをさらに便利にするTips\u003c/h2\u003e\u003col\u003e\u003cli\u003e\u003cstrong\u003eスプレッドシートと連携\u003c/strong\u003e:\u003cul\u003e\u003cli\u003eスプレッドシートにスクレイピング対象のURLリストを記載し、1つずつ処理する方法。\u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003cli\u003e\u003cstrong\u003eエラー処理の実装\u003c/strong\u003e:\u003cul\u003e\u003cli\u003eサイトがダウンしている場合やHTML構造が変更された場合でもエラーにならないように工夫します。\u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003c/ol\u003e\u003cpre\u003e\u003ccode class=\"language-javascript\"\u003efunction scrapeWithErrorHandling() {\n try {\n var url = \"https://example.com\";\n var response = UrlFetchApp.fetch(url);\n var html = response.getContentText();\n var title = html.match(/\u003ctitle\u003e([\\s\\S]*?)\u003c\\/title\u003e/)[1];\n Logger.log(\"タイトル: \" + title);\n } catch (e) {\n Logger.log(\"エラーが発生しました: \" + e.message);\n }\n}\n\u003c/code\u003e\u003c/pre\u003e\u003chr\u003e\u003ch2 id=\"ha214098e44\"\u003eまとめ\u003c/h2\u003e\u003cp\u003eGASを使ったWebスクレイピングは、初心者から上級者まで活用できる便利なツールです。無料で始められるうえにGoogleサービスとの親和性が高い点が魅力です。この記事で紹介したコード例を活用し、自分だけのスクレイピングツールを作成してみましょう。\u003c/p\u003e\u003cp\u003e\u003cstrong\u003e今すぐ試してみたい方\u003c/strong\u003eは、この記事を参考にGASプロジェクトを作成し、ぜひ実践してみてください!\u003c/p\u003e"])</script><script>self.__next_f.push([1,"14:[\"$\",\"div\",null,{\"className\":\"max-w-4xl mx-auto px-6 pb-8 __className_e8ce0c\",\"children\":[\"$\",\"article\",null,{\"className\":\"prose prose-lg max-w-none\",\"children\":[\"$\",\"div\",null,{\"className\":\"article-body space-y-6\",\"children\":[false,[\"$\",\"div\",null,{\"dangerouslySetInnerHTML\":{\"__html\":\"$17\"}}],\"$L18\"]}]}]}]\n15:[\"$\",\"div\",null,{\"style\":{\"display\":\"block\",\"padding\":\"30px 15px\",\"maxWidth\":\"520px\",\"margin\":\"0 auto\"},\"children\":[\"$\",\"$L5\",null,{\"href\":\"https://bug-fix.org/siid\",\"target\":\"_blank\",\"children\":[\"$\",\"$L11\",null,{\"src\":\"/banner-1.png\",\"width\":300,\"height\":300,\"alt\":\"プログラミングスクール・SiiDの情報はこちら\",\"style\":{\"width\":\"100%\",\"height\":\"auto\"}}]}]}]\n"])</script><script>self.__next_f.push([1,"18:[\"$\",\"div\",null,{\"className\":\"pt-10 mt-25 border-t border-gray-300 border-solid\",\"children\":[false,[\"$\",\"p\",null,{\"children\":\"もし、\"}],[\"$\",\"p\",null,{\"children\":[\"「プログラミングを体系的に学びたい」\",[\"$\",\"br\",null,{}],\"「エンジニア転職を頑張りたい」\",[\"$\",\"br\",null,{}],\"「独学に限界を感じてきた...」\",[\"$\",\"br\",null,{}],\"「コミュニティで仲間と共に学びたい」\"]}],[\"$\",\"p\",null,{\"children\":\"などと感じられたら、ぜひ検討してみてください。\"}],[\"$\",\"p\",null,{\"children\":[\"$\",\"$L5\",null,{\"href\":\"https://bug-fix.org/siid/counseling\",\"target\":\"_blank\",\"children\":\"個別面談・説明会はこちら!\"}]}],[\"$\",\"br\",null,{}],[\"$\",\"p\",null,{\"children\":[\"まずは様子見...という方は、公式LINEにぜひご登録下さい。\",[\"$\",\"br\",null,{}],\"学習や転職ノウハウに関する\",[\"$\",\"b\",null,{\"children\":\"豪華特典11個を無料配布\"}],\"しています!\",[\"$\",\"br\",null,{}],[\"$\",\"$L5\",null,{\"href\":\"https://bug-fix.org/siid-lp\",\"target\":\"_blank\",\"children\":\"LINE紹介ページで特典を確認する\"}]]}],[\"$\",\"br\",null,{}],[\"$\",\"p\",null,{\"children\":[\"$\",\"$L5\",null,{\"href\":\"https://www.youtube.com/@programming-siid\",\"target\":\"_blank\",\"children\":\"■YouTube(SiiD受講生さま実績)\"}]}],[\"$\",\"p\",null,{\"children\":[\"$\",\"$L5\",null,{\"href\":\"https://www.youtube.com/@webit7652\",\"target\":\"_blank\",\"children\":\"■YouTube(セイト先生メイン)\"}]}],[\"$\",\"p\",null,{\"children\":[\"$\",\"$L5\",null,{\"href\":\"https://x.com/seito_horiguchi\",\"target\":\"_blank\",\"children\":\"■X(旧Twitter)\"}]}]]}]\n"])</script></body></html>