defaults.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import re
  2. __all__ = ['cleaner']
  3. # 独立元素
  4. INDEPENDENT_TAGS = {
  5. '<head>[\s\S]*?</head>': '',
  6. '<html>|<html [^>]*>|</html>': '',
  7. '<body>|<body [^>]*>|</body>': '',
  8. '<meta[^<>]*>|<meta [^<>]*>|<meta[^<>]*>[\s\S]*?</meta>|</meta>': '', # 元数据
  9. '&(nbsp|e[mn]sp|thinsp|zwn?j|#13);': '', # 空格
  10. '\\xa0|\\u3000': '', # 空格
  11. '<!--[\s\S]*?-->': '', # 注释
  12. '<style[^<>]*>[\s\S]*?</style>': '', # 样式
  13. '<script[^<>]*>[\s\S]*?</script>': '', # JavaScript
  14. '<input>': '', # 输入框
  15. '<img[^>]*>': '<br>', # 图片
  16. }
  17. # 行内元素
  18. INLINE_TAGS = {
  19. '<a>|<a [^>]*>|</a>': '', # 超链接
  20. '<span>|<span [^>]*>|</span>': '', # span
  21. '<label>|<label [^>]*>|</label>': '<br>', # label
  22. '<font>|<font [^>]*>|</font>': '', # font
  23. }
  24. # 块级元素
  25. BLOCK_TAGS = {
  26. '<h[1-6][^>]*>|</h[1-6]>': '', # 标题
  27. '<p>|<p [^>]*>|</p>': '<br>', # 段落
  28. '<div>|<div [^>]*>|</div>': '<br>', # 分割 division
  29. '<o:p>|<o:p [^>]*>|</o:p>': '' # OFFICE微软WORD段落
  30. }
  31. # 其他
  32. OTHER = {
  33. '<?xml[^>]*>|<?xml [^>]*>|<?xml:.*?>': '',
  34. '<epointform>': '',
  35. '<!doctype html>|<!doctype html [^>]*>': '',
  36. '【关闭】|关闭': '',
  37. '【打印】|打印本页': '',
  38. '【字体:[\s\S]*】': '',
  39. '文章来源:[\u4e00-\u9fa5]+': '',
  40. '浏览次数:.*[<]+': '',
  41. '(责任编辑:.*?)': '',
  42. '分享到[:]': '',
  43. }
  44. # 样式
  45. CSS_STYLE = {
  46. 'style="[\s\S]*?"|style ="[\s\S]*?"': '',
  47. 'bgcolor="[\s\S]*?"|bgcolor ="[\s\S]*?"': '',
  48. 'bordercolor="[\s\S]*?"|bordercolor ="[\s\S]*?"': '',
  49. 'class="[\s\S]*?"|class ="[\s\S]*?"': '',
  50. 'align="[\s\S]*?"|align ="[\s\S]*?"': '',
  51. 'cellpadding="(\d+)"|cellspacing="(\d+)"': '',
  52. }
  53. # 空白符
  54. BLANKS = {
  55. '\n\s*\n': '\n',
  56. '\s*\n\s*': '\n',
  57. '[^\S\n]': ' ',
  58. '\s+': ' ',
  59. }
  60. # css标签集合
  61. TAGS = {'table', 'tr', 'td', 'div', 'span', 'p'}
  62. # css属性集合
  63. ATTRS = {'id', 'class', 'style', 'width'}
  64. def _repair_tag():
  65. """异常的标签组合,用来替换非标准页面的标签"""
  66. _repairs = {}
  67. for tag in TAGS:
  68. for attr in ATTRS:
  69. key = '{}{}'.format(tag, attr)
  70. val = '{} {}'.format(tag, attr)
  71. _repairs[key] = val
  72. return _repairs
  73. def _escape_character(html):
  74. """转义字符"""
  75. html = html.replace('&lt;', '<')
  76. html = html.replace('&gt;', '>')
  77. html = html.replace('&quot;', '"')
  78. html = html.replace('&amp;', '&')
  79. return html
  80. def _lowercase_tag(html):
  81. """标签归一化处理(全部小写)"""
  82. tags = re.findall("<[^>]+>", html)
  83. for tag in tags:
  84. html = html.replace(tag, str(tag).lower())
  85. repair_tags = _repair_tag()
  86. for err, right in repair_tags.items():
  87. html = html.replace(err, right)
  88. return html
  89. def cleaner(html, special=None, completely=False):
  90. """
  91. 数据清洗
  92. :param html: 清洗的页面
  93. :param special: 额外指定页面清洗规则
  94. :param completely: 是否完全清洗页面
  95. :return: 清洗后的页面源码
  96. """
  97. if special is None:
  98. special = {}
  99. OTHER.update(special)
  100. remove_tags = {
  101. **INDEPENDENT_TAGS,
  102. **INLINE_TAGS,
  103. **BLOCK_TAGS,
  104. **OTHER,
  105. **CSS_STYLE,
  106. **BLANKS,
  107. }
  108. html = _lowercase_tag(html)
  109. for tag, repl in remove_tags.items():
  110. html = re.sub(tag, repl, html)
  111. if completely:
  112. html = re.sub(r'<canvas[^<>]*>[\s\S]*?</canvas>', '', html) # 画布
  113. html = re.sub(r'<iframe[^<>]*>[\s\S]*?</iframe>', '', html) # 内框架
  114. html = re.sub('<([^<>\u4e00-\u9fa5]|微软雅黑|宋体|仿宋)+>', '', html)
  115. html = _escape_character(html)
  116. return html