In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.