diff options
author | Sergey M․ <dstftw@gmail.com> | 2015-09-04 23:56:45 +0600 |
---|---|---|
committer | Sergey M․ <dstftw@gmail.com> | 2015-09-04 23:56:45 +0600 |
commit | a41fb80ce1e2fec48d7a4bc15d169061e76ca672 (patch) | |
tree | 3d700e5a8700b65e86bbe315ce5e130d84fd49ed /youtube_dl/utils.py | |
parent | 2e2575e2135a22e62cb4148f7424df54d59efb2a (diff) |
[utils] Add xpath_element and xpath_attr
Diffstat (limited to 'youtube_dl/utils.py')
-rw-r--r-- | youtube_dl/utils.py | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index b7a423166..de5069c7b 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -176,7 +176,7 @@ def xpath_with_ns(path, ns_map): return '/'.join(replaced) -def xpath_text(node, xpath, name=None, fatal=False, default=NO_DEFAULT): +def xpath_element(node, xpath, name=None, fatal=False, default=NO_DEFAULT): if sys.version_info < (2, 7): # Crazy 2.6 xpath = xpath.encode('ascii') @@ -189,7 +189,24 @@ def xpath_text(node, xpath, name=None, fatal=False, default=NO_DEFAULT): raise ExtractorError('Could not find XML element %s' % name) else: return None - return n.text + return n + + +def xpath_text(node, xpath, name=None, fatal=False, default=NO_DEFAULT): + return xpath_element(node, xpath, name, fatal=fatal, default=default).text + + +def xpath_attr(node, xpath, key, name=None, fatal=False, default=NO_DEFAULT): + n = find_xpath_attr(node, xpath, key) + if n is None: + if default is not NO_DEFAULT: + return default + elif fatal: + name = '%s[@%s]' % (xpath, key) if name is None else name + raise ExtractorError('Could not find XML attribute %s' % name) + else: + return None + return n.attrib[key] def get_element_by_id(id, html): |