Python正则表达式匹配HTML页面编码
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=["']?content-type["']?[ ]+content=["']?text/html;[ ]*charset=([0-9-a-zA-Z]+)["']?" 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'
相关推荐
RuoShangM 2020-02-27
Buerzhu 2019-12-02
dailinqing 2019-10-28
chenwen0000 2019-09-07
shijinling0 2019-06-29
LHpython 2019-04-23
JamesHammer 2019-04-17
PythonBiglove 2018-12-25
JamesHammer 2019-04-05
pythonpycharm 2013-12-25
yoyoshadow 2013-11-20
impressyourcat 2018-03-22