mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-11-24 13:46:53 +01:00
[utils] Improve xpath_text
This commit is contained in:
parent
22889ab175
commit
8e636da499
@ -181,7 +181,7 @@ def xpath_element(node, xpath, name=None, fatal=False, default=NO_DEFAULT):
|
|||||||
xpath = xpath.encode('ascii')
|
xpath = xpath.encode('ascii')
|
||||||
|
|
||||||
n = node.find(xpath)
|
n = node.find(xpath)
|
||||||
if n is None or n.text is None:
|
if n is None:
|
||||||
if default is not NO_DEFAULT:
|
if default is not NO_DEFAULT:
|
||||||
return default
|
return default
|
||||||
elif fatal:
|
elif fatal:
|
||||||
@ -193,7 +193,18 @@ def xpath_element(node, xpath, name=None, fatal=False, default=NO_DEFAULT):
|
|||||||
|
|
||||||
|
|
||||||
def xpath_text(node, xpath, name=None, fatal=False, default=NO_DEFAULT):
|
def xpath_text(node, xpath, name=None, fatal=False, default=NO_DEFAULT):
|
||||||
return xpath_element(node, xpath, name, fatal=fatal, default=default).text
|
n = xpath_element(node, xpath, name, fatal=fatal, default=default)
|
||||||
|
if n is None or n == default:
|
||||||
|
return n
|
||||||
|
if n.text is None:
|
||||||
|
if default is not NO_DEFAULT:
|
||||||
|
return default
|
||||||
|
elif fatal:
|
||||||
|
name = xpath if name is None else name
|
||||||
|
raise ExtractorError('Could not find XML element\'s text %s' % name)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
return n.text
|
||||||
|
|
||||||
|
|
||||||
def xpath_attr(node, xpath, key, name=None, fatal=False, default=NO_DEFAULT):
|
def xpath_attr(node, xpath, key, name=None, fatal=False, default=NO_DEFAULT):
|
||||||
|
Loading…
Reference in New Issue
Block a user