Python crawler--regular expression of several methods of parsing web pages

Source: Internet
Author: User

1. Regular expressions

A regular expression is a special sequence of characters that can help you easily check whether a string matches a pattern.

The RE module enables the Python language to have all the regular expression functionality.

Re.match function

Re.match attempts to match a pattern from the starting position of the string, and if the match is not successful, match () returns none.

Import Re Print (Re.match ('www'www.runoob.com'). span ())  # match at start position Print (Re.match ('com'www.runoob.com'))         # Do not match at start position

Results:

(0, 3) None
Importreline="Cats is smarter than dogs"Matchobj= Re.match (r'(. *) is (. *?). *', line)ifMatchobj:Print("Matchobj.group ():", Matchobj.group ())Print("Matchobj.group (1):", Matchobj.group (1))   Print("Matchobj.group (2):", Matchobj.group (2))Else:   Print("No match!!")

Results:

Matchobj.group ():  Cats is smarter than Dogsmatchobj.group (1):  catsmatchobj.group (2):  Smarter
R ' (. *) is (. *). * ', r means raw string, pure string, group (0), is the overall result of the matching regular expression, group (1) lists the first bracket matching part, Group (2) lists the second bracket matching part.

Re.search method

Re.search scans the entire string and returns the first successful match.

Re.match matches only the beginning of the string, if the string does not begin to conform to the regular expression, the match fails, the function returns none, and Re.search matches the entire string until a match is found.

Importreline="Cats is smarter than dogs"; Matchobj= Re.match (r'Dogs', line, re. m|Re. I)ifMatchobj:Print("match--matchobj.group ():", Matchobj.group ())Else:   Print("No match!!") Matchobj= Re.search (r'Dogs', line, re. m|Re. I)ifMatchobj:Print("Search--Matchobj.group ():", Matchobj.group ())Else:   Print("No match!!")

Results:

-Matchobj.group ():  dogs

Re.findall method

FindAll is able to find the matching result and return it as a list.

ImportRequestsImportrelink="http://www.sohu.com/"Headers= {'user-agent':'mozilla/5.0 (Windows; U Windows NT 6.1; En-us; rv:1.9.1.6) gecko/20091201 firefox/3.5.6'}r= Requests.get (link, headers=headers) HTML=r.texttitle_list= Re.findall ('href= ". *?". <strong> (. *?) </strong>', HTML)Print(title_list)
[' News ', ' finance ', ' Sports ', ' real estate ', ' entertainment ', ' cars ', ' fashion ', ' technology ', ' gourmet ', ' constellation ', ' email ', ' map ', ' thousand sails ', ' swim ']

Grab the main title of Sohu.




Python crawler--regular expression of several methods of parsing web pages

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.