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 = 'baidu' 我们的需求是通过正则表达式提取网址和锚文本,那可以用到findall()

import rerelink = '(.*)'info = 'baidu'cinfo = re.findall(relink,info)print cinfo

输出的结果:[('http://www.baidu.com', 'baidu')] 返回的是一个列表,列表里面是匹配的结果形成的元组形式。如果你需要用正则替换的话,可以看下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('(.*?)',sitemap)
#download each link
for link in links:
html=download(link)