From 6d4a90e8d1229a5d4f21f1d88b239214fcf5b19f Mon Sep 17 00:00:00 2001 From: Maud LAURENT Date: Wed, 19 May 2021 17:34:50 +0200 Subject: [PATCH] fix: move exception. Delegate encoding to lxml if not specified --- nvchecker_source/htmlparser.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/nvchecker_source/htmlparser.py b/nvchecker_source/htmlparser.py index e519478..96f1767 100644 --- a/nvchecker_source/htmlparser.py +++ b/nvchecker_source/htmlparser.py @@ -14,18 +14,17 @@ async def get_version(name, conf, *, cache, **kwargs): async def get_version_impl(info): conf = dict(info) - encoding = conf.get('encoding', 'latin1') + encoding = conf.get('encoding') + parser = html.HTMLParser(encoding=encoding) res = await session.get(conf['url']) - body = html.fromstring(res.body.decode(encoding)) + doc = html.fromstring(res.body, base_url=conf['url'], parser=parser) + try: - checkxpath = body.xpath(conf.get('xpath')) - except etree.XPathEvalError as e: - raise GetVersionError('bad xpath', exc_info=e) - - try: - version = body.xpath(conf.get('xpath')) + version = doc.xpath(conf.get('xpath')) except ValueError: if not conf.get('missing_ok', False): raise GetVersionError('version string not found.') - return version \ No newline at end of file + except etree.XPathEvalError as e: + raise GetVersionError('bad xpath', exc_info=e) + return version