物理の駅 Physics station by 現役研究者

テクノロジーは共有されてこそ栄える

Google検索結果をJSONで保存するブックマークレット

Google検索で表示される最大10件のウェブサイトの、タイトル、URL、説明を保存するためのブックマークレットを作った。

以下のスクリプトをコピーして、ブックマークのURL部分にペーストして保存する。Google検索してからこのブックマークを押すと、結果をJSONファイルとして保存するための「名前をつけて保存」が表示される。

javascript: (function () { function downloadJSON(data, filename) { let blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }); let url = URL.createObjectURL(blob); let a = document.createElement('a'); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); document.body.removeChild(a); } function getPageNumber() { let startParam = new URLSearchParams(window.location.search).get('start'); let pageNumber = startParam ? (parseInt(startParam) / 10) + 1 : 1; return String(pageNumber).padStart(3, "0"); } let results = []; document.querySelectorAll("div.g").forEach(el => { let titleElement = el.querySelector("h3"); let linkElement = el.querySelector("a"); let snippetElement = el.querySelector(".VwiC3b"); if (titleElement && linkElement) { results.push({ title: titleElement.innerText, url: linkElement.href, snippet: snippetElement ? snippetElement.innerText : "" }); } }); let pageNumber = getPageNumber(); let filename = `google_search_results_${pageNumber}.json`; if (results.length > 0) { downloadJSON(results, filename); } else { alert("検索結果が見つかりません。"); } })();

2025年3月23日 更新

javascript: (function () { function downloadJSON(data, filename) { const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); document.body.removeChild(a); } function getPageNumber() { const startParam = new URLSearchParams(window.location.search).get('start'); const pageNumber = startParam ? (parseInt(startParam, 10) / 10) + 1 : 1; return String(pageNumber).padStart(3, "0"); } const results = []; document.querySelectorAll("div.tF2Cxc").forEach(el => { const titleElement = el.querySelector("h3"); const linkElement = el.querySelector("a"); const snippetElement = el.querySelector(".VwiC3b, .lyLwlc, .MUxGbd"); if (titleElement && linkElement) { results.push({ title: titleElement.innerText, url: linkElement.href, snippet: snippetElement ? snippetElement.innerText : "" }); } }); const pageNumber = getPageNumber(); const filename = `google_search_results_${pageNumber}.json`; if (results.length > 0) { downloadJSON(results, filename); } else { alert("検索結果が見つかりません。"); } })();