pythonpython的正则表达式式怎么提取

python使用正则表达式提取网页URL的方法
作者:八大山人
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了python使用正则表达式提取网页URL的方法,涉及Python中urllib模块及正则表达式的相关使用技巧,需要的朋友可以参考下
本文实例讲述了python使用正则表达式提取网页URL的方法。分享给大家供大家参考。具体实现方法如下:
import urllib
url="http://www.jb51.net"
s=urllib.urlopen(url).read()
ss=s.replace(" ","")
urls=re.findall(r"&a.*?href=.*?&\/a&",ss,re.I)
for i in urls:
print 'this is over'
希望本文所述对大家的Python程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具二次元同好交流新大陆
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
测试结果:&勉强能用。。哈哈。。。。
阅读(5286)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'python正则表达式获取网址、IP、手机号、邮箱',
blogAbstract:'\t取之于网,整理之后回报以网。#!/usr/bin/env python#-*- coding:utf-8 -*-import reimport sysdef getIPAddFromFile(fobj):
regex = re.compile(r\'\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b\', re.IGNORECASE)',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:1,
publishTime:4,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:1,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}python正则表达式提取
python正则表达式提取
&span class="tg-floor-sold" max-join="99838"&已售55980&/span&
中的55980,文中"99838"值会发生变化
按投票排序
谁没事用正则做html解析……xpath://span[@class="tg-floor-sold"]/text()
来学易语言吧局_正则.创建 (“&已售(\d{3,})&/span& ”, 假)
(?&=已售).*(?=&/span&) 不用管max-join的值可以么?
OK,终于理解你什么意思了!Bentham的答案看起来可行,但是Python遇到中文往往得不到好结果。所以这是我的答案:max-join.*?&.*?(\d+)&手机打字,没办法测试,不过应该可行更新图片,证明是可行的~
已有帐号?
无法登录?
社交帐号登录Python中正则表达式的用法实例汇总 - 突袭新闻
当前位置&:&&&&Python中正则表达式的用法实例汇总
热门标签:
Python中正则表达式的用法实例汇总
来源:本网整理
编辑:王可评论:
这篇文章主要介绍了Python中正则表达式的用法实例汇总,非常实用,需要的朋友可以参考下
正则表达式是Python程序设计中非常实用的功能,本文就常用的正则表达式做一汇总,供大家参考之用。具体如下:
一、字符串替换
1.替换所有匹配的子串
用newstring替换subject中所有与正则表达式regex匹配的子串
result, number = re.subn(regex, newstring, subject)
2.替换所有匹配的子串(使用正则表达式对象)
reobj = re.compile(regex)
result, number = reobj.subn(newstring, subject)
二、字符串拆分
1.字符串拆分
result = re.split(regex, subject)
2.字符串拆分(使用正则表示式对象)
reobj = re.compile(regex)
result = reobj.split(subject)
下面列出Python正则表达式的几种匹配用法:
1.测试正则表达式是否匹配字符串的全部或部分
regex=ur"..." #正则表达式
if re.search(regex, subject):
do_something()
do_anotherthing()
2.测试正则表达式是否匹配整个字符串
regex=ur"...\Z" #正则表达式末尾以\Z结束
if re.match(regex, subject):
do_something()
do_anotherthing()
3. 创建一个匹配对象,然后通过该对象获得匹配细节
regex=ur"..." #正则表达式
match = re.search(regex, subject)
# match start: match.start()
# match end (exclusive): match.end()
# matched text: match.group()
do_something()
do_anotherthing()
4.获取正则表达式所匹配的子串
(Get the part of a string matched by the regex)
regex=ur"..." #正则表达式
match = re.search(regex, subject)
result = match.group()
result = ""
5. 获取捕获组所匹配的子串
(Get the part of a string matched by a capturing group)
regex=ur"..." #正则表达式
match = re.search(regex, subject)
result = match.group(1)
result = ""
6. 获取有名组所匹配的子串
(Get the part of a string matched by a named group)
regex=ur"..." #正则表达式
match = re.search(regex, subject)
result = match.group("groupname")
result = ""
7. 将字符串中所有匹配的子串放入数组中
(Get an array of all regex matches in a string)
result = re.findall(regex, subject)
8.遍历所有匹配的子串
(Iterate over all matches in a string)
for match in re.finditer(r"&(.*?)\s*.*?/\1&", subject)
# match start: match.start()
# match end (exclusive): match.end()
# matched text: match.group()
9.通过正则表达式字符串创建一个正则表达式对象
(Create an object to use the same regex for many operations)
reobj = re.compile(regex)
10.用法1的正则表达式对象版本
(use regex object for if/else branch whether (part of) a string can be matched)
reobj = re.compile(regex)
if reobj.search(subject):
do_something()
do_anotherthing()
11.用法2的正则表达式对象版本
(use regex object for if/else branch whether a string can be matched entirely)
reobj = re.compile(r"\Z") #正则表达式末尾以\Z 结束
if reobj.match(subject):
do_something()
do_anotherthing()
12.创建一个正则表达式对象,然后通过该对象获得匹配细节
(Create an object with details about how the regex object matches (part of) a string)
reobj = re.compile(regex)
match = reobj.search(subject)
# match start: match.start()
# match end (exclusive): match.end()
# matched text: match.group()
do_something()
do_anotherthing()
13.用正则表达式对象获取匹配子串
(Use regex object to get the part of a string matched by the regex)
reobj = re.compile(regex)
match = reobj.search(subject)
result = match.group()
result = ""
14.用正则表达式对象获取捕获组所匹配的子串
(Use regex object to get the part of a string matched by a capturing group)
reobj = re.compile(regex)
match = reobj.search(subject)
result = match.group(1)
result = ""
15.用正则表达式对象获取有名组所匹配的子串
(Use regex object to get the part of a string matched by a named group)
reobj = re.compile(regex)
match = reobj.search(subject)
result = match.group("groupname")
result = ""
16.用正则表达式对象获取所有匹配子串并放入数组
(Use regex object to get an array of all regex matches in a string)
reobj = re.compile(regex)
result = reobj.findall(subject)
17.通过正则表达式对象遍历所有匹配子串
(Use regex object to iterate over all matches in a string)
reobj = re.compile(regex)
for match in reobj.finditer(subject):
# match start: match.start()
# match end (exclusive): match.end()
# matched text: match.group()
感兴趣的读者可以动手调试一下本文实例代码,相信会有新的收获。
初创公司,跑不快你就死了
百度迎史上最大变革:血拼O2O 冷对华尔街
百度值多少钱?华尔街这次可能错了
本网最新文章Python 正则表达式re模块使用_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Python 正则表达式re模块使用
来源:Linux社区&
作者:gccmx
re是Python的正则表达式模块,在此记录实际开发过程中的使用。正则表达式的最佳实践是使用re.compile方法编译后使用,效率会更好
1、re.search()example:TS .15-02-04 14:24:25& 14:24:25 up 82 days, 23:51,& 2 users,& load average: 0.02, 0.03, 0.10
匹配 TS开头跟一个空格后跟若干个数字。&&& import re &&& str="TS .15-02-04 14:24:25& 14:24:25 up 82 days, 23:51,& 2 users,& load average: 0.02, 0.03, 0.10"&&& import re &&& prog = re.compile(r'^TS \d+') &&& m = prog.search(str) &&& m.group(0) 'TS '
2、re.split()拆分字符串是很常见的需求,通常使用split方法,但是一次指定多个分割符,split方法不支持,只能用re的split方法了example:TS .15-02-04 14:24:25& 14:24:25 up 82 days, 23:51,& 2 users,& load average: 0.02, 0.03, 0.10以空格和.拆分上面的字符串123 &&& str="TS .15-02-04 14:24:25& 14:24:25 up 82 days, 23:51,& 2 users,& load average: 0.02, 0.03, 0.10"&&& re.split(r'\s+|\.',str) ['TS', '', '', '', '14:24:25', '14:24:25', 'up', '82', 'days,', '23:51,', '2', 'users,', 'load', 'average:', '0', '02,', '0', '03,', '0', '10']
--------------------------------------分割线 --------------------------------------
上源码安装Python3.4&
《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版]
《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码]
Python脚本获取Linux系统信息
在下用Python搭建桌面算法交易研究环境
Python 语言的发展简史
Python 的详细介绍:Python 的下载地址:
本文永久更新链接地址:
相关资讯 & & &
& (03月24日)
& (01月15日)
& (04月09日)
& (03月18日)
& (12/21/:20)
图片资讯 & & &
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款}

我要回帖

更多关于 python正则表达式提取 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信