[utils] Support attributes with no values in get_elements_by_attribute()
authorYen Chi Hsuan <yan12125@gmail.com>
Wed, 5 Jul 2017 15:23:35 +0000 (23:23 +0800)
committerYen Chi Hsuan <yan12125@gmail.com>
Wed, 5 Jul 2017 15:27:12 +0000 (23:27 +0800)
test/test_utils.py
youtube_dl/utils.py

index 2b93b360477f857b8fa693450d3c5375db5663cc..30738e7cf4e84ad32f895f21eb59dace2f717b7e 100644 (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', '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):
         html = '''
             <span class="foo bar">nice</span><span class="foo bar">also nice</span>
index 39860e9d156baf2ceb205460c5f78fa2e9026f52..fdf5e29e7d417b94ba7f83129a8420f93ed31217 100644 (file)
@@ -365,9 +365,9 @@ def get_elements_by_attribute(attribute, value, html, escape_value=True):
     retlist = []
     for m in re.finditer(r'''(?xs)
         <([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+[a-zA-Z0-9:._-]+(?:=[a-zA-Z0-9:._-]*|="[^"]*"|='[^']*'))*?
+         (?:\s+[a-zA-Z0-9:._-]+(?:=[a-zA-Z0-9:._-]*|="[^"]*"|='[^']*'|))*?
         \s*>
         (?P<content>.*?)
         </\1>