正则表达式匹配(python)
获取图片的python代码
#coding=utf-8
import urllib
import re def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html def getImg(html):
reg = r'src="(.+?\.jpg).+"'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl,'%s.jpg' % x)
x+=1 html = getHtml("http://www.csdn.net/article/2015-01-15/2823564") print getImg(html)
findall和group的用法
import re
reg = r'www\.(.*)\..{3}'
imgre = re.compile(reg)
imglist = re.findall(imgre,'www.python.org')
#for imgurl in imglist:
print imglist
import re
reg = r'(.+):"(.+a)"'
imgre = re.compile(reg)
imglist = re.findall(imgre,'name:"wangjian"ok')
#for imgurl in imglist:
print imglist
import re
reg = r'src="(.+?\.jpg)"'
imgre = re.compile(reg)
html='<img src="http://cms.csdnimg.cn/article/201501/15/54b70da54b668_middle.jpg?_=48735" style="float: none; margin: 0px;" alt="">'
imglist = re.findall(imgre,html)
print imglist
参考https://docs.python.org/2/library/re.html#re.findall
python的search和match的区别
精通正则表达式第三版
解释了在正则表达式中\b元字符的使用的参考文档如下:
http://www.cnblogs.com/85538649/archive/2011/07/26/wtq0705.html
http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaocheng.html
正则表达式匹配(python)的更多相关文章
- [转载]Python正则表达式匹配反斜杠'\'问题
转载自csdnblog:Python正则表达式匹配反斜杠'\'问题 在学习Python正则式的过程中,有一个问题一直困扰我,如何去匹配一个反斜杠(即“\”)? 一.引入 在学习了Python特殊字符和 ...
- Python: 正则表达式匹配反斜杠 "\"
Python正则表达式匹配反斜杠 "\" eg: >>>a='w\w\w' 'w\\w\\w' # 打印出来的 "\\" 被转义成 一个反斜 ...
- 正则表达式匹配可以更快更简单 (but is slow in Java, Perl, PHP, Python, Ruby, ...)
source: https://swtch.com/~rsc/regexp/regexp1.html translated by trav, travmymail@gmail.com 引言 下图是两种 ...
- Python 使用正则表达式匹配IP信息
使用正则表达式匹配IP地址 .MAC地址 .网卡名称: #!/usr/bin/env python #-*- coding:utf-8 -*- import re from subprocess im ...
- Python从文件中读取字符串,用正则表达式匹配中文字符的问题
2013-07-27 21:01:37| 在Windows下,用Python从.txt文件中读取字符串,并用正则表达式匹配中文,在网上看了方法,用的时候发现中文没有被匹配. ...
- Python用正则表达式匹配汉字
Python用正则表达式匹配汉字 匹配多个汉字,不包括空格 import re res = re.match(r'[\u4E00-\u9FA5]+', '我是 汉字') print(res) # &l ...
- Python妙用re.sub分析正则表达式匹配过程
声明:本文所使用方法为老猿自行研究并编码,相关代码版权为老猿所有,禁止转载文章,代码禁止用于商业用途! 在<第11.23节 Python 中re模块的搜索替换功能:sub及subn函数>介 ...
- Python 使用正则表达式匹配URL网址
使用正则表达式匹配以 .com 或 .cn 为域名后缀的URL地址 In [1]: import re In [2]: str = "http://www.baidu.com/" ...
- Python 正则表达式 匹配次数
管道可以匹配多个正则表达式中的一个 >>> >>> m=re.search(r'Batman|Tina Fey','Batman and Tina Fey')> ...
- python 正则表达式匹配IP地址
一.实验环境 1.Windows7x64_SP1 2.anaconda2.5.0 + python2.7(anaconda集成,不需单独安装) 3.pyinstaller3.0 二.实验目的 从tex ...
随机推荐
- Python的subprocess模块
尝试在Python中运行可执行文件,网上给出的解决方案是: import os os.system("此处填程序路径") 我要运行的程序文件名中有空格,因此果断失败了,查看了一下帮 ...
- div+css知识点
前端书写规范: 1.所有书写均在英文半角状态下的小写: 2.id,class必须以字母开头: 3.所有标签必须闭合: 4.html标签用tab键缩进: 5.属性值必须带引号: 6.<!-- ht ...
- c++ 顺序容器学习 - 容器适配器
摘要: 对 容器适配器 的疑问. 刚开始接触 容器适配器 时,总感觉怪怪的,认为多此一举,顺手搜了搜,原来我在这一点is not alone: STL容器适配器的用途 其中有个老兄说的好,这里 引用一 ...
- "Ray, Pass me the dishes!"
uvaLive3938:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pag ...
- XE5 Android 开发数据访问server端
http://www.cnblogs.com/key-ok/p/3326064.html http://www.cnblogs.com/key-ok/p/3326055.html http://www ...
- Delphi编程中资源文件的应用
Delphi编程中资源文件的应用/转自 http://chamlly.spaces.live.com/blog/cns!548f73d8734d3acb!236.entry一.引子: 现在的Windo ...
- Android Wear开发 - 数据通讯 - 第三节 : 事件处理
http://developer.android.com/training/wearables/data-layer/events.html 以下是本人在学习官方开发文档时的笔记,主要是翻译为主,并在 ...
- Maven实战二
简述: 现需要 在Eclipse中配置Maven插件 安装maven应用,配置Maven环境变量 建立Maven管理的工程 用Maven导入Gson包, 编写简易Json输出程序 步骤: 1. 首先到 ...
- “adb server is out of date. killing.... ADB server didn't ACK * failed to start daemon * ”
草泥马的adb: “adb server is out of date. killing.... ADB server didn't ACK * failed to start daemon * ” ...
- 老毛桃U盘启动盘制作工具V20140501完美贡献版
老毛桃U盘启动盘制作工具V20140501完美贡献版 下载地址:http://down.laomaotao.net:90/LaoMaoTao_V2014zhuangji.exe 老毛桃U盘装系统综合教 ...