Last active
October 4, 2024 06:03
-
-
Save wadako111/ccc08ba710328dad7dd4cdc69f700658 to your computer and use it in GitHub Desktop.
住信SBIネット銀行のMEGA BIGのくじ番号を取得するコードです。ログイン後 https://toto.netbk.co.jp/main/smh0100 この画面でchrome developer toolsで実行することで自動でCSVファイルとして保存されます。冒頭の最大ページ数だけ自分の画面に表示されているものに変更してください。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 10を自分の最大ページ数に変える | |
const maxPage = 10; | |
const pageNumbers = Array.from({ length: maxPage }, (v, i) => i + 1); | |
// 詳細ページからCSVを作成 | |
function generateCSV(doc) { | |
// 各行のデータを格納する配列 | |
const rows = []; | |
// テーブル内の行を選択 | |
const tableRows = doc.querySelectorAll("table.tbl._result tbody tr"); | |
// 各行からデータを抽出し、配列に格納 | |
tableRows.forEach(row => { | |
const predictions = Array.from(row.querySelectorAll("td")).slice(4, 14).map(td => td.textContent.trim()); | |
rows.push(predictions); | |
}); | |
// 行列を転置する関数 | |
function transpose(matrix) { | |
return matrix[0].map((_, colIndex) => matrix.map(row => row[colIndex])); | |
} | |
// 行列を転置 | |
const transposedRows = transpose(rows); | |
// 転置されたデータをCSV形式に変換 | |
const csvContent = transposedRows.map(row => row.join(",")).join("\n"); | |
return csvContent; | |
} | |
// 一覧ページからすべての詳細ページを取得してCSVを作成 | |
const getCsvFromListPage = async (doc) => { | |
let urls = []; | |
doc.querySelectorAll('td.__action a').forEach(link => { | |
urls.push(link.href); | |
}); | |
let csv = ""; | |
for (const link of urls) { | |
await fetch(link) | |
.then(response => { | |
if (!response.ok) { | |
throw new Error('Network response was not ok'); | |
} | |
return response.text(); | |
}) | |
.then(htmlString => { | |
// HTMLをDOMに変換する | |
const parser = new DOMParser(); | |
const doc2 = parser.parseFromString(htmlString, 'text/html'); | |
const c = generateCSV(doc2); | |
csv = csv.concat(c).concat("\n"); | |
}) | |
.catch(error => { | |
console.error('There has been a problem with your fetch operation:', error); | |
}); | |
}; | |
return csv; | |
} | |
let csvContent = ""; | |
for (number of pageNumbers) { | |
const pageUrl = `https://toto.netbk.co.jp/main/smh0100!paging?currentPage=${number}&holdCount=0#purchaseList`; | |
const response = await fetch(pageUrl) | |
if (!response.ok) { | |
throw new Error('Network response was not ok'); | |
} | |
// HTMLをDOMに変換する | |
const parser = new DOMParser(); | |
const doc = parser.parseFromString(await response.text(), 'text/html'); | |
const c = await getCsvFromListPage(doc); | |
csvContent = csvContent.concat(c); | |
console.log(`page: ${number}/${pageNumbers.at(-1)}`); | |
} | |
// ファイルに保存 | |
const downloadLink = document.createElement("a"); | |
downloadLink.href = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csvContent); | |
downloadLink.target = '_blank'; | |
downloadLink.download = 'transposed_predictions.csv'; | |
downloadLink.click(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment