Regular expressions are used extensively in text-matching. The web crawler often involves extracting some information from the page, and regular expressions can greatly simplify the process of filtering information.
Learn about regular expressions you can refer to http://www.runoob.com/python/python-reg-expressions.html we use the regular expression of a mailbox as an example to introduce the application of regular expressions. An email address can be broken down into the following rules:
- the first part of the email address includes at least one content: uppercase letters, lowercase letters, numbers 0-9, Dot (.), plus (+), or underscore (_), and the corresponding regular expression is [a-za-z0-9\._+]+
- after that, an @ symbol is included, and after @, the email address contains at least one uppercase or lowercase letter, corresponding to the regular type [a-za-z]+ , and then includes a dot number, ending with a com, org, edu, net domain name , (com|org|edu|net)
- By summarizing these rules, you can get a regular expression that matches the mailbox:
[A-za-z0-9\._+][email protected][a-za-z]+\. ( com|org|edu|net) with this expression we can easily parse out the email address in the Web page without having to write a lot of character judgments
. Regular expressions can be used not only independently, but also in BeautifulSoup, in fact, most functions that support string parameters can be implemented using regular expressions.
as in BeautifulSoup's Find function, find ("img", {"src": re.compile ("xxx")} can be used to find an image that conforms to the rule.
From for notes (Wiz)
Web crawler (4)--Regular expression