How to use regular expressions in crawlers
This article is about how regular expressions are used in crawlers. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Regular expressions: common symbols for learning regular expressions
2. Re module: learn how to use re module in python
3. Combined application of Requests and re modules: case description
Regular expression:
General characters:
Predefined characters:
Quantifier:
Boundary matching:
(?) The parentheses return the result,.? Match any character
Import re
A = 'xxIxxmexxlovexxsffaxxpythonxx'
Infos = re.findall ('xx (. *?) xx',a)
Print (infos)
Output result: Ipaw love, python
Re module and method thereof
Search () function: matches and extracts the first content that conforms to the rule, and returns a regular expression object
Re.match (pattern,string,flags=0)
Where:
(1) pattern is a matching regular expression
(2) string is the string to be matched
(3) flags is the flag bit, which is used to control the matching mode of regular expressions, such as case sensitivity, multi-line matching, etc.
Import re
Axiomane1two2three3'
Infos=re.search ('\ dflowers, a)
Print (infos)
Output:
Import re
Axiomane1two2three3'
Infos=re.search ('\ dflowers, a)
Print (infos.group ())
Output: 1
Sub () function: used to replace matches in a string
Re.sub (pattern,repl,string,count=0,flags=0)
Where:
(1) pattern is a matching regular expression
(2) repl is a replacement string
(3) string is the original string to be found and replaced
(4) counts is the maximum number of substitutions after pattern matching. Default 0 means replacing all matches.
(5) flags is the flag bit, which is used to control the matching mode of regular expressions, such as case sensitivity, multi-line matching, etc.
Import re
Phone='123-456789'
New_phone=re.sub ('\ Dawns, girls, phones, phones)
Print (new_phone)
Output: 123456789
The findall () function: matches everything that conforms to the rule and returns the result as a list.
Import re
Axiomane1two2three3'
Infos2=re.findall ('\ dflowers, a)
Print (infos2)
Output: ['1','2','3']
Import re
A =''index
''
Word = re.findall ('(. *?)', a, re.S)
Print (word [0] .strip ())
Output: index
Thank you for reading! This is the end of this article on "how to use regular expressions in reptiles". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!