瀏覽代碼

[utlis] add extract_attributes for extracting html tags attributes

remitamine 10 年之前
父節點
當前提交
689fb748ee
共有 1 個文件被更改,包括 8 次插入0 次删除
  1. 8 0
      youtube_dl/utils.py

+ 8 - 0
youtube_dl/utils.py

@@ -248,6 +248,14 @@ def get_element_by_attribute(attribute, value, html):
     return unescapeHTML(res)
 
 
+def extract_attributes(attributes_str, attributes_regex=r'(?s)\s*([^\s=]+)\s*=\s*["\']([^"\']+)["\']'):
+    attributes = re.findall(attributes_regex, attributes_str)
+    attributes_dict = {}
+    if attributes:
+        attributes_dict = {attribute_name: attribute_value for (attribute_name, attribute_value) in attributes}
+    return attributes_dict
+
+
 def clean_html(html):
     """Clean an HTML snippet into a readable string"""