ראיתי את הספריה israeli-bank-scrapers
התלבטתי אם להשתמש בזה.
אמרתי שאני ינסה ויראה עד כמה זה מורכב לממש את זה בעצמי.
תכלס כתבתי את זה עם עזרת GPT, וקיבלתי תוצאה טובה. (יש לי יותר מידע ממה שהם מחזירים, ואני יכול גם להוסיף כל הזמן כל מידע שאני צריך)
מצרף את הקובץ
const puppeteer = require('puppeteer');
const axios = require('axios');
const fs = require('fs');
const poalim = async (userCode, password, tranStartDate = null, tranEndDate = null) => {//dates in format yyyymmdd
// פתיחת הדפדפן במצב דיבוג (headful)
const browser = await puppeteer.launch({
headless: false, // הופך את המצב ל-Headful
});
const pages = await browser.pages();
const page = pages[0]; // שימוש בטאב הראשון
await page.goto("https://login.bankhapoalim.co.il/ng-portals/auth/he/");
// הזנת מידע לתוך האינפוטים
await page.type('#userCode', userCode); // הזנת טקסט לאינפוט הראשון (לפי מזהה id)
await page.type('#password', password); // הזנת טקסט לאינפוט השני (לפי מזהה id)
// לחיצה על כפתור מסוג submit עם הטקסט "כניסה"
await page.evaluate(() => {
const button = Array.from(document.querySelectorAll('button[type="submit"]'))
.find(el => el.innerText === 'כניסה');
if (button) button.click();
});
await page.waitForNavigation();
// שליפת עוגיות
const cookies = await page.cookies();
// יצירת מחרוזת עוגיות עבור בקשות HTTP
const cookieHeader = cookies.map(cookie => `${cookie.name}=${cookie.value}`).join('; ');
// שליחת בקשה HTTP עם העוגיות
const accounts = await axios.get('https://login.bankhapoalim.co.il/ServerServices/general/accounts', {
headers: {
'Cookie': cookieHeader, // שימוש בעוגיות שהתקבלו
},
});
// קביעת תאריכים עם ברירת מחדל אם לא סופקו
const startDate = tranStartDate || new Date(new Date().setFullYear(new Date().getFullYear() - 1)).toISOString().split('T')[0].replace(/-/g, '');
const endDate = tranEndDate || new Date().toISOString().split('T')[0].replace(/-/g, '');
// עיבוד כל החשבונות במקביל
const enrichedAccounts = await Promise.all(
accounts.data.map(async (account) => {
const accountId = `${account.bankNumber}-${account.branchNumber}-${account.accountNumber}`;
// בקשה לפרטי היתרה והקרדיט
const metadataPromise = axios.get(
`https://login.bankhapoalim.co.il/ServerServices/current-account/composite/balanceAndCreditLimit?accountId=${accountId}&view=details&lang=he`,
{ headers: { 'Cookie': cookieHeader } }
);
// בקשה לרשימת תנועות
const transactionsPromise = axios.get(
`https://login.bankhapoalim.co.il/ServerServices/current-account/transactions?accountId=${accountId}&numItemsPerPage=1000&retrievalEndDate=${endDate}&retrievalStartDate=${startDate}&sortCode=1`,
{ headers: { 'Cookie': cookieHeader } }
);
// מחכים לשתי הבקשות במקביל
const [metadataResponse, transactionsResponse] = await Promise.all([metadataPromise, transactionsPromise]);
// החזרת האובייקט המועשר
return {
...account,
metadata: metadataResponse.data,
transactions: transactionsResponse.data,
};
})
);
const now = new Date().toISOString().replace(/[-:T]/g, '').slice(0, 8) + '_' + new Date().toTimeString().slice(0, 5).replace(/:/g, '');
// כתיבת התוצאה לקובץ JSON
fs.writeFileSync(`bank_${now}.json`, JSON.stringify(enrichedAccounts, null, 2));
await page.deleteCookie(...cookies);// מחיקת עוגיות
await browser.close(); // סגירת הדפדפן
}
poalim('userName', 'password');