python使用正则表达式分析网页中的图片并进行替换的方法

yipeiwu_com5年前Python基础

本文实例讲述了python使用正则表达式分析网页中的图片并进行替换的方法。分享给大家供大家参考。具体分析如下:

这段代码分析网页中的所有图片表单<img>,分析后为其前后添加相应的修饰标签,并添加到图片的超级链接。

复制代码 代码如下:
result = value.replace("[ page ]","").replace('  ',u' ')
p=re.compile(r'''(<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*["']?[\s\t\r\n]*([^\s\t\r\n"'<>]*)[^<>]*?/?[\s\t\r\n]*>)''',re.IGNORECASE)
result = p.sub(r'''<span class="openIcon"><em></em><a href="\2">\1</a></span>''',result)

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg

希望本文所述对大家的Python程序设计有所帮助。

相关文章

浅谈django的render函数的参数问题

hello.html 文件代码如下: HelloWorld/templates/hello.html 文件代码: <h1>{{ hello }}</h1>...

Python 3.6打包成EXE可执行程序的实现

Python 3.6打包成EXE可执行程序的实现

1、下载pyinstaller python 3.6 已经自己安装了pip,所以只需要执行 pip install pyinstaller就可以了 2、打包程序 进入到你你需要打包的目...

python实现文件路径和url相互转换的方法

本文实例讲述了python实现文件路径和url相互转换的方法。分享给大家供大家参考。具体实现方法如下: import urllib pathname = 'path/to/file...

Python之dict(或对象)与json之间的互相转化实例

在Python语言中,json数据与dict字典以及对象之间的转化,是必不可少的操作。 在Python中自带json库。通过import json导入。 在json模块有2个方法, lo...

python从子线程中获得返回值的方法

如下所示: # coding:utf-8 import time from threading import Thread def foo(number): time.s...