Home > Backend Development > Python Tutorial > Python正则表达式匹配HTML页面编码

Python正则表达式匹配HTML页面编码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 11:24:04
Original
1188 people have browsed it

html页面一般都会指定一个编码,如何获取到是处理html页面的第一步,因为错误的编码必然带来后面处理的问题。这里我用python的正则表达式写了个:

import re

a = ["<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />",
   '<meta http-equiv=Content-Type content="text/html;charset=gb2312">',
   '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">',
   '<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />',
   '<meta http-equiv="content-type" content="text/html; charset=utf-8" />',
   '<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />',
   '<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />'
   ]



b = "<meta[ ]+http-equiv=["']&#63;content-type["']&#63;[ ]+content=["']&#63;text/html;[ ]*charset=([0-9-a-zA-Z]+)["']&#63;"


B = re.compile(b, re.IGNORECASE)


for ax in a:
  r1 = B.search(ax)

  if r1:
    print r1.group()
    print r1.group(1), len(r1.group())
  else:
    print 'not match'
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template