def fetch(self, url, retry=3):
'''?? URL?????? HTML ??'''
try:
start_time = self.loop.time()
response = await aiohttp.request('GET', url)
time_used = self.loop.time() - start_time
except (TimeoutError, aiohttp.ClientResponseError) as e:
# ??
if retry > 0:
retry -= 1
await asyncio.sleep(1)
return await self.fetch(url, retry)
else:
time_used = self.loop.time() - start_time
logger.error('USE %6.3f s STAT: 500 URL: %s (%s)'
% (time_used, url, e))
return ''
except Exception as e:
time_used = self.loop.time() - start_time
logger.error('USE %6.3f s STAT: 500 URL: %s (%s)'
% (time_used, url, e))
return ''
if not (200 <= response.status < 300):
logger.error('USE %6.3f s STAT: %s URL: %s'
% (time_used, response.status, url))
# ?? html ??????
body = await response.read()
try:
return body.decode('utf-8')
except UnicodeDecodeError:
try:
return body.decode('gbk')
except UnicodeDecodeError:
return body
评论列表
文章目录