在html源代码中 图像是用,使用python在html源代码中查找图像

没有什么错的正则表达式,你缺少两样东西:

Python没有一个正则表达式类型,所以你必须把它包装在一个字符串中。使用raw字符串,以便字符串按原样传递给正则表达式编译器,无需任何转义解释

.read()调用的结果是字节序列,而不是字符串。所以你需要一个字节序列正则表达式。

第二个是Python3特定的(而且我知道你正在使用PY 3)

把所有在一起,就解决这样的上述行:

pat = re.compile (rb']*src="([^"]+)')

r代表原字节序列为b。

时,你不会找到任何东西在这里,我们去:

Python 3.3.2+

Type "help", "copyright", "credits" or "license" for more information.

>>> import urllib.request

>>> import re

>>> website = urllib.request.urlopen('http://stackoverflow.com/')

>>> html = website.read()

>>> pat = re.compile (rb']*src="([^"]+)')

>>> img = pat.findall(html)

>>> img

[b'http://i.stack.imgur.com/tKsDb.png', b'http://i.stack.imgur.com/dmHl0.png', b'http://i.stack.imgur.com/dmHl0.png', b'http://i.stack.imgur.com/tKsDb.png', b'http://i.stack.imgur.com/6QN0y.png', b'http://i.stack.imgur.com/tKsDb.png', b'http://i.stack.imgur.com/L8rHf.png', b'http://i.stack.imgur.com/tKsDb.png', b'http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif']

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值