Python Regular expression 1

Source: Internet
Author: User

The RE module provides an engine interface for the Zheng expression, which allows your re string to be mutated into an object and used for matching, which is a high efficiency. Let's take a look at some examples.


The following is a small script that I wrote similar to reptiles, for your reference

#!/usr/bin/python

# Coding=utf-8


Import Urllib

Import re

Import OS


Def gethtmldata ():

Return Urllib.urlopen ("http://news.cnblogs.com/n/93182"). Read ()


Def getimagelist ():

#reg1 = Re.compile (R ' (http.*jpg) \ "T ')

REG2 = Re.compile (R ' src=\ "(http.*jpg) \")

Text = Gethtmldata ()

#list1 = Reg1.findall (text)

List2 = Reg2.findall (text)

x = 30

Print OS.GETCWD ()

For IMAGEURL in List2:

Print ImageUrl

Urllib.urlretrieve (IMAGEURL, R "jpg/%s.jpg"% x)

X+=1


GetImageList ()

REG2 = Re.compile (R ' src=\ "(http.*jpg) \") matches "src=" at the beginning, "The end of the character, the http.*jpg in parentheses is the content to be obtained, and such an expression is compiled into an object


In the example, the "http://news.cnblogs.com/n/93182" content in the page is attached. The function of this little script is to find the high-definition picture in the page and download it to the JPG folder of the current directory.


This article comes from "on the road ... "Blog, be sure to keep this provenance http://yongbird.blog.51cto.com/7554149/1548471

Python Regular expression 1

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.