没有什么错的正则表达式,你缺少两样东西:
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']