테스트 케이스 추가 및 1차 시도 실패시 2차 시도
This commit is contained in:
1 parent
b7edba8c80
commit
2e1ccebe43
3 files changed
+60
-18
No files matched your search
@@ -10,6 +10,10 @@ class nvMapPwScraper():
|
||||
_context = None
|
||||
_win_width = 1280
|
||||
_win_height = 720
|
||||
_max_retry = 30 # place id timeout threshold seconds
|
||||
|
||||
# instance var
|
||||
page = None
|
||||
|
||||
@classmethod
|
||||
def default_context_builder(cls):
|
||||
@@ -41,11 +45,15 @@ class nvMapPwScraper():
|
||||
def __init__(self):
|
||||
if not self.is_ready:
|
||||
raise Exception("nvMapScraper is not initiated")
|
||||
|
||||
|
||||
async def __aenter__(self):
|
||||
await self.create_page()
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
await self.page.close()
|
||||
|
||||
async def create_page(self):
|
||||
while(not self.is_ready):
|
||||
asyncio.sleep(1000)
|
||||
self.page = await self._context.new_page()
|
||||
await self.page.add_init_script(
|
||||
'''const defaultGetter = Object.getOwnPropertyDescriptor(
|
||||
@@ -77,9 +85,9 @@ patchedGetter.toString();''')
|
||||
})
|
||||
await self.page.goto("http://google.com")
|
||||
|
||||
async def goto_url(self, url):
|
||||
async def goto_url(self, url, wait_until="domcontentloaded", timeout=20000):
|
||||
page = self.page
|
||||
await page.goto(url, wait_until="domcontentloaded", timeout=20000)
|
||||
await page.goto(url, wait_until=wait_until, timeout=timeout)
|
||||
|
||||
async def get_place_id_url(self, selected):
|
||||
|
||||
@@ -88,13 +96,18 @@ patchedGetter.toString();''')
|
||||
encoded_query = parse.quote(f"{address} {title}")
|
||||
url = f"https://map.naver.com/p/search/{encoded_query}"
|
||||
|
||||
await self.goto_url(url)
|
||||
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
|
||||
|
||||
count = 0
|
||||
while(count < 5):
|
||||
if "isCorrectAnswer=true" in self.page.url:
|
||||
return self.page.url
|
||||
await asyncio.sleep(1)
|
||||
count += 1
|
||||
if "/place/" in self.page.url:
|
||||
return self.page.url
|
||||
|
||||
raise Exception("Failed to identify place id. item is ambiguous")
|
||||
url = self.page.url.replace("?","?isCorrectAnswer=true&")
|
||||
await self.goto_url(url, wait_until="networkidle",timeout = self._max_retry/2*1000)
|
||||
|
||||
if "/place/" in self.page.url:
|
||||
return self.page.url
|
||||
|
||||
if (count == self._max_retry / 2):
|
||||
raise Exception("Failed to identify place id. loading timeout")
|
||||
else:
|
||||
raise Exception("Failed to identify place id. item is ambiguous")
|
||||
Reference in new issue
Block a user