첫 완료
This commit is contained in:
123
fetch-articles-pc.ts
Normal file
123
fetch-articles-pc.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import axios from "axios";
|
||||
import { realtorIds } from "./config";
|
||||
import axiosRetry from "axios-retry";
|
||||
|
||||
axiosRetry(axios, {
|
||||
retries: 4, // 4번 재시도
|
||||
retryDelay: (retryCount: number) => {
|
||||
console.log(`재시도 ${retryCount}번째 시도 중...`);
|
||||
return retryCount * 2000; // 2초, 4초, 6초, 8초 대기
|
||||
},
|
||||
retryCondition: (error: any) => {
|
||||
// 네트워크 에러 또는 5xx 에러일 때 재시도
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
const main = async () => {
|
||||
const startTime = Date.now(); // 시작 시간 기록
|
||||
const realtorId = "namyeong00";
|
||||
|
||||
const headers = {
|
||||
accept: "*/*",
|
||||
"accept-language": "ko;q=0.7",
|
||||
authorization:
|
||||
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IlJFQUxFU1RBVEUiLCJpYXQiOjE3NjExMzM1NDUsImV4cCI6MTc2MTE0NDM0NX0.MJv-3xWZeWHCahmq0w5hxlZNmr7qwu1bOEaV2rvuthY",
|
||||
"cache-control": "no-cache",
|
||||
pragma: "no-cache",
|
||||
priority: "u=1, i",
|
||||
referer:
|
||||
"https://new.land.naver.com/houses?ms=37.6560144,126.7916037,15&a=DDDGG:JWJT:SGJT:VL&e=RETAIL&articleNo=2556801691&realtorId=s9055515",
|
||||
"sec-ch-ua": '"Brave";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
|
||||
"sec-ch-ua-mobile": "?0",
|
||||
"sec-ch-ua-platform": '"Windows"',
|
||||
"sec-fetch-dest": "empty",
|
||||
"sec-fetch-mode": "cors",
|
||||
"sec-fetch-site": "same-origin",
|
||||
"sec-gpc": "1",
|
||||
"user-agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36",
|
||||
Cookie:
|
||||
"PROP_TEST_ID=7e02e4c10eb2f2c2358bca876c0d7ad0b41ee920553cf666f9f8ffea510fe421; PROP_TEST_KEY=1758430291866.2e161026d60e6da4b6ffc456ba20732ac32391c0294df00f3c0b051eb574277e",
|
||||
};
|
||||
|
||||
// 1. 첫 번째 요청으로 총 개수 가져오기
|
||||
console.log("총 매물 개수 확인 중...");
|
||||
const firstResponse = await axios.get(
|
||||
`https://new.land.naver.com/api/articles?realEstateType=&tradeType=&order=rank&page=1&zoom=0&realtorId=${realtorId}`,
|
||||
{
|
||||
proxy: {
|
||||
host: "gw.dataimpulse.com",
|
||||
port: 823,
|
||||
auth: {
|
||||
username: "0bdeb90b7713c370cdeb__cr.kr",
|
||||
password: "a5ae50d6913bd778",
|
||||
},
|
||||
},
|
||||
headers,
|
||||
}
|
||||
);
|
||||
|
||||
const mapExposedCount = firstResponse.data.mapExposedCount;
|
||||
console.log(`총 매물 개수: ${mapExposedCount}`);
|
||||
|
||||
// 2. 총 페이지 수 계산 (한 페이지당 20개)
|
||||
const totalPage = Math.ceil(mapExposedCount / 20);
|
||||
console.log(`총 페이지 수: ${totalPage}`);
|
||||
|
||||
// 3. 모든 페이지 번호 배열 생성
|
||||
const pages = Array.from({ length: totalPage }, (_, i) => i + 1);
|
||||
|
||||
// 4. 10개씩 동시 요청
|
||||
const allArticles: any[] = [];
|
||||
const concurrency = 1; // 동시 요청 수
|
||||
|
||||
// 설명
|
||||
for (let i = 0; i < pages.length; i += concurrency) {
|
||||
const chunk = pages.slice(i, i + concurrency);
|
||||
console.log(
|
||||
`페이지 ${chunk[0]} ~ ${chunk[chunk.length - 1]} 요청 중... (${
|
||||
i + 1
|
||||
}-${Math.min(i + concurrency, pages.length)}/${pages.length})`
|
||||
);
|
||||
|
||||
const promises = chunk.map((page) =>
|
||||
axios.get(
|
||||
`https://new.land.naver.com/api/articles?realEstateType=&tradeType=&order=rank&page=${page}&zoom=0&realtorId=${realtorId}`,
|
||||
{
|
||||
proxy: {
|
||||
host: "gw.dataimpulse.com",
|
||||
port: 823,
|
||||
auth: {
|
||||
username: "0bdeb90b7713c370cdeb__cr.kr",
|
||||
password: "a5ae50d6913bd778",
|
||||
},
|
||||
},
|
||||
headers,
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
const responses = await Promise.all(promises);
|
||||
|
||||
responses.forEach((response, index) => {
|
||||
const articles = response.data.articleList || [];
|
||||
console.log(`페이지 ${chunk[index]}: ${articles.length}개 매물`);
|
||||
allArticles.push(...articles);
|
||||
});
|
||||
|
||||
// 다음 배치 전에 잠시 대기 (API 부하 방지)
|
||||
if (i + concurrency < pages.length) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
}
|
||||
}
|
||||
|
||||
const endTime = Date.now(); // 종료 시간 기록
|
||||
const elapsedTime = ((endTime - startTime) / 1000).toFixed(2); // 초 단위로 변환
|
||||
|
||||
console.log(`\n총 ${allArticles.length}개 매물 수집 완료`);
|
||||
console.log(`소요 시간: ${elapsedTime}초`);
|
||||
console.log(allArticles[0]); // 첫 번째 매물 샘플 출력
|
||||
};
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user