votes up 9

Could not find XML element's text %s

Package:
Exception Class:
ExtractorError

Raise code

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):
    n = find_xpath_attr(node, xpath, key)

Ways to fix

votes up 1 votes down

Error code:

from youtube_dl.compat import compat_etree_fromstring
from youtube_dl.utils import xpath_text

testxml = '''<root>
            <div>
            </div>
        </root>'''
ÔĽŅ
doc = compat_etree_fromstring(testxml)

xpath_text(doc, 'div/p',fatal=True)

Explanation:

The error is raised when using xpath_text. It extracts text from a given XPath based on given tags. In the above example, we will get an error because we don't have a <p> tag to parse.

To avoid an error,

1) change the fatal parameter to True

2) pass exact tags to parse

Fix code:

from youtube_dl.compat import compat_etree_fromstring
from youtube_dl.utils import xpath_text

testxml = '''<root>
            <div>
                <p>Foo</p>
            </div>
        </root>'''
doc = compat_etree_fromstring(testxml)

xpath_text(doc, 'div/p',fatal=True)
Aug 05, 2021 anonim answer
anonim 13.0k

Add a possible fix

Please authorize to post fix