Python regular expression to obtain, filter, or replace HTML tags. python labels

Source: Internet
Author: User
Tags html comment

Python regular expression to obtain, filter, or replace HTML tags. python labels

This article introduces several methods for getting, filtering, or replacing HTML tags using regular expressions in Python. The specific content is as follows:

Key Content of python Regular Expressions:

Python Regular Expression escape character:

. Match any character except linefeed \ w match letters, numbers, underscores, or Chinese characters \ s match any blank character \ d match digits \ B match word start or end ^ match string start $ end of matching string \ W match any letter, number, underline, chinese character \ S match any character not blank character \ D match any non-numeric character \ B match not the start or end position of the word [^ x] match any character except x [^ aeiou] matches any character except aeiou.

Common python Regular Expression qualifier code/syntax description:

* Repeated zero or more times + one or more times? Zero or one {n} repeat n times {n,} repeat n times or more {n, m} repeat n to m times

Python Regular Expression naming group:

Naming group :(? P <name>...) This article also mentions the definition (starting with a question mark, there is a '<' in the forward direction, and a 'in other words '! '): Forward definition (? <= ...) Backward definition (? = ...) Backward non-defined (? <!....) Backward non-defined (?!.....)

Python uses regular expressions to get, remove (filter), or replace HTML Tag code examples

1. Python uses a regular expression to retrieve html Zhongtian information. Example code:

#! /Usr/bin/env python #-*-coding: utf8-*-import re html = "

2. Python uses regular expressions to remove (filter) HTML Tag sample code:

#-*-Coding: UTF-8-*-import re # filter tags in HTML # Remove tags and other information in HTML # @ param htmlstr HTML string. def filter_tags (htmlstr): # filter CDATA re_cdata = re. compile ('// <! \ [CDATA \ [[^>] * // \]> ', re. i) # match CDATA re_script = re. compile ('<\ s * script [^>] *> [^ <] * <\ s */\ s * script \ s *>', re. i) # Script re_style = re. compile ('<\ s * style [^>] *> [^ <] * <\ s */\ s * style \ s *>', re. i) # style re_br = re. compile ('<br \ s *? /?> ') # Re_h = re. compile (' </? \ W + [^>] *> ') # HTML Tag re_comment = re. compile (' <! -- [^>] * --> ') # HTML comment s = re_cdata.sub ('', htmlstr) # Remove CDATA s = re_script.sub ('', s) # Remove SCRIPT s = re_style.sub ('', s) # Remove style s = re_br.sub ('\ n', s) # convert br to line feed s = re_h.sub ('', s) # Remove the HTML Tag s = re_comment.sub (', s) # Remove the HTML comment # Remove the extra blank line (blank line = re. compile ('\ n +') s = blank_line.sub ('\ n', s) s = replaceCharEntity (s) # Replace entity return s # Replace common HTML character entities. # use normal characters to replace special character entities in HTML. # You can add new entity characters to CHAR_ENTITIES to process more HTML characters. # @ param htmlstr HT ML string. def replaceCharEntity (htmlstr): CHAR_ENTITIES = {'nbsp ': '', '000000':'', 'lt': '<', '60': '<', 'gt ':'> ', '62':'> ', 'am':' & ', '38':' & ', 'quot ':'"', '34': '"',} re_charEntity = re. compile (R '&#? (? P <name> \ w +); ') sz = re_charEntity.search (htmlstr) while sz: entity = sz. group () # Full name of entity, such as> key = sz. group ('name') # Remove &; after entity, for example,> For gt try: htmlstr = re_charEntity.sub (CHAR_ENTITIES [key], htmlstr, 1) sz = re_charEntity.search (htmlstr) failed t KeyError: # use an empty string instead of htmlstr = require ('', htmlstr, 1) sz = re_charEntity.search (htmlstr) return htmlstrdef repalce (s, re_exp, repl_string): return re_exp.sub (repl_string, s) if _ name __= = '_ main _': s1_file('169it.com_index.htm '). read () news = filter_tags (s) print news

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • Python regular expressions match HTML page Encoding
  • How to remove all html tags using python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.