Browse Source

[utils] Support attributes with no values in get_elements_by_attribute()

master-ytdl-org
Yen Chi Hsuan 8 years ago
parent
commit
609ff8ca19
2 changed files with 8 additions and 2 deletions
  1. +6
    -0
      test/test_utils.py
  2. +2
    -2
      youtube_dl/utils.py

+ 6
- 0
test/test_utils.py View File

@ -1228,6 +1228,12 @@ part 3</font></u>
self.assertEqual(get_element_by_attribute('class', 'foo', html), None) self.assertEqual(get_element_by_attribute('class', 'foo', html), None)
self.assertEqual(get_element_by_attribute('class', 'no-such-foo', html), None) self.assertEqual(get_element_by_attribute('class', 'no-such-foo', html), None)
html = '''
<div itemprop="author" itemscope>foo</div>
'''
self.assertEqual(get_element_by_attribute('itemprop', 'author', html), 'foo')
def test_get_elements_by_class(self): def test_get_elements_by_class(self):
html = ''' html = '''
<span class="foo bar">nice</span><span class="foo bar">also nice</span> <span class="foo bar">nice</span><span class="foo bar">also nice</span>


+ 2
- 2
youtube_dl/utils.py View File

@ -365,9 +365,9 @@ def get_elements_by_attribute(attribute, value, html, escape_value=True):
retlist = [] retlist = []
for m in re.finditer(r'''(?xs) for m in re.finditer(r'''(?xs)
<([a-zA-Z0-9:._-]+) <([a-zA-Z0-9:._-]+)
(?:\s+[a-zA-Z0-9:._-]+(?:=[a-zA-Z0-9:._-]*|="[^"]*"|='[^']*'))*?
(?:\s+[a-zA-Z0-9:._-]+(?:=[a-zA-Z0-9:._-]*|="[^"]*"|='[^']*'|))*?
\s+%s=['"]?%s['"]? \s+%s=['"]?%s['"]?
(?:\s+[a-zA-Z0-9:._-]+(?:=[a-zA-Z0-9:._-]*|="[^"]*"|='[^']*'))*?
(?:\s+[a-zA-Z0-9:._-]+(?:=[a-zA-Z0-9:._-]*|="[^"]*"|='[^']*'|))*?
\s*> \s*>
(?P<content>.*?) (?P<content>.*?)
</\1> </\1>


Loading…
Cancel
Save