cleaner.py 3.8 KB

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