当前位置:首页 - 建站教程 - python教程 - pythn 正则表达式 re findall 返回能匹配的字符串

pythn 正则表达式 re findall 返回能匹配的字符串

时间:2018-11-27来源: 作者: 文章热度:
点评:python 正则表达式 re findall 方法能够以列表的形式返回能匹配的子串。re.findall(pattern, string[, flags]):搜索string,以列表形式返回全部能匹配的子串。先看个简单的代
...python 正则表达式 re findall 方法能够以列表的形式返回能匹配的子串。

re.findall(pattern, string[, flags]):

搜索string,以列表形式返回全部能匹配的子串。先看个简单的代码:

import re
 
p = re.compile(r'\d+')
print p.findall('one1two2three3four4')
 
### output ###
# ['1', '2', '3', '4']

稍微复杂点比如:
info = '<a href="http://www.sfk8.com">帝国仿站</a>' 我们的需求是通过正则表达式提取网址和锚文本,那可以用到
findall()

import re
relink = '<a href="(.*)">(.*)</a>'
info = '<a href="http://www.sfk8.com">帝国仿站</a>'
cinfo = re.findall(relink,info)
print cinfo

输出的结果:[('http://www.sfk8.com', '帝国仿站')] 返回的是一个列表,列表里面是匹配的结果形成的元组形式。如果你需要用正则替换的话,可以看下python re sub

 

 

以下是一个网站地图爬虫 ,其中用到了re.findall 语法

import urllib2
import re
def download(url,user_agent='wswp', num_retries=2):
    print 'downloading:',url
    headers={'User-agent':user_agent}
    request=urllib2.Request(url,headers=headers)
    try:
         html=urllib2.urlopen(url).read()
    except urllib2.URLError as e:
        print 'download error:', e.reason
        html=None
        if num_retries>0:
            if hasattr(e, 'code') and  500<=e.code<600:
                #recursively retry 5XX http errors
                return download(url, user_agent,num_retries-1)
    return html

def crawl_sitemap(url):
    #download the sitemap file
    sitemap=download(url)
    #extract the sitemap links
    links = re.findall('<loc>(.*?)</loc>',sitemap)
    #download each link
    for link in links:
        html=download(link)
上一篇:返回列表
相关python教程
最新python教程