正则表达式匹配(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中xrange与range的异同
转载自:http://ciniao.me/article.php?id=17 >>> range(5) [0, 1, 2, 3, 4] >>> range(1, 5 ...
- JavaScript 项目构建工具 Grunt 实践:安装和创建项目框架
Grunt 是一个基于任务的 JavaScript 项目命令行构建工具,运行于 Node.js 平台.Grunt 能够从模板快速创建项目,合并.压缩和校验 CSS & JS 文件,运行单元测 ...
- [BZOJ 1576] [Usaco2009 Jan] 安全路经Travel 【树链剖分】
题目链接: BZOJ - 1576 题目分析 首先Orz Hzwer的题解. 先使用 dijikstra 求出最短路径树. 那么对于一条不在最短路径树上的边 (u -> v, w) 我们可以先沿 ...
- IIC协议及其对ACK应答信号的处理
1,SCL一直由Master控制,SDA依照数据传送的方向,读数据时由Slave控制SDA,写数据时由Master控制SDA.当8位数据传送完毕之后,应答位或者否应答位的SDA控制权与数据位传送时相反 ...
- Word Pattern
package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** * * @au ...
- A Round Peg in a Ground Hole(判断是否是凸包,点是否在凸包内,圆与多边形的关系)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4628 Accepted: 1434 Description The D ...
- bzoj2724
分块大法好!首先预处理第i块到第j块的答案,这是可以在O(n*tot)内处理出来的 tot表示块数然后考虑询问对于[l,r],答案只可能是[l,r]之间所夹整块[i,j]的答案和非整块中的位置上的数下 ...
- (转载)eclipse 快捷键大全,eclipse查找类,文件,添加注释
(转载)http://hi.baidu.com/fegro/item/8224c8c28b174627ee466598 /* ----------------------------------- ...
- 图论(2-sat):HDU 3062 Party
Party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
B. Complete The Graph time limit per test 4 seconds memory limit per test 256 megabytes input standa ...