python 正则表达式 之re.findall
来源:互联网 发布:淘宝宝贝图片宽度 编辑:程序博客网 时间:2024/05/21 13:57
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.baidu.com">baidu</a>' 我们的需求是通过正则表达式提取网址和锚文本,那可以用到
findall()
import re
relink = '<a href="(.*)">(.*)</a>'
info = '<a href="http://www.baidu.com">baidu</a>'
cinfo = re.findall(relink,info)
print cinfo
relink = '<a href="(.*)">(.*)</a>'
info = '<a href="http://www.baidu.com">baidu</a>'
cinfo = re.findall(relink,info)
print cinfo
输出的结果:[('http://www.baidu.com', 'baidu')] 返回的是一个列表,列表里面是匹配的结果形成的元组形式
例子function:
def get_yizhe_info(url): # url weiyigecanshu #url='http://book.douban.com/subject/6082808/?from=tag_all' # For Test req = urllib2.Request(url, headers=hds2[np.random.randint(0,len(hds2))]) html = urllib2.urlopen(req).read() html = html.replace('\n', '') #print html try: reg_temp='<span class="pl"> 译者</span>: *?(.*?)<br/>' temp=re.findall(reg_temp,html) print temp if temp: reg_author = '<a class="" href=".*?">(.*?)</a>' name = re.findall(reg_author, temp[0]) print name str='' for na in name: str=na+'/'+str str = str.replace(str,str[:-1]) except: str = '暂无' return str
0 0
- python 正则表达式 之re.findall
- python 正则表达式re.findall
- python 正则表达式re.findall
- 正则表达式 re.findall 用法
- python正则表达式 re (二)findall与finditer
- Python正则表达式学习(5)——re.findall()
- Python中正则表达式re模块-compile()和findall()
- python正则表达式-findall
- python 正则表达式 () findall
- Python 正则re模块之compile()和findall()详解
- python之正则表达式匹配全部findall()方法
- python re 正则表达式之二
- python学习之2 正则表达式re
- Python之re(正则表达式)模块小结
- Python 之 re模块 正则表达式
- 20161003 Python笔记之正则表达式 re
- Python基础学习之re正则表达式
- python模块之re正则表达式
- Maven报错: Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp
- Moto Z Force拍照性能得到87分,DxOMark惹争议
- php 编写一个简单的模板引擎
- 设置套接字选项值setsockopt()函数使用详解
- 远程重启服务器
- python 正则表达式 之re.findall
- HDOJ-2124之Repair the Wall
- 微信刮刮乐
- php iframe 父级页面跳转 登陆页面跳转
- GDB的一些小技巧
- POJ 2192 Zipper
- 怎么取出服务端发过来的数据所对应的值
- Android Button Maker:在线生成按钮代码的工具
- UITABBAR特殊样式设置