>  Q&A  >  본문

Python怎么匹配HTML页面编码?

Python怎么匹配HTML页面编码?

baby不要哭泣baby不要哭泣2787일 전1179

모든 응답(2)나는 대답할 것이다

  • 数据分析师

    数据分析师2017-10-01 00:39:27

    Python은 HTML 페이지 인코딩과 어떻게 일치하나요? -PHP 중국어 웹사이트 Q&A-파이썬은 HTML 페이지 인코딩과 어떻게 일치하나요? -PHP 중국어 홈페이지 Q&A

    꼭 보고 배워보세요.

    회신하다
    0
  • 伊谢尔伦

    伊谢尔伦2017-03-03 10:54:32

    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'


    회신하다
    0
  • 취소회신하다