我试图从html页面中匹配<span>标记中的端口号:
<span class="tbBottomLine" style="width:50px;">
8080
</span>
<span class = "tbBottomLine" style = "width: 50px;">
80
</ span>
<span class = "tbBottomLine" style = "width: 50px;">
3124
</ span>
<span class = "tbBottomLine" style = "width: 50px;">
1142
</ span>剧本:
import urllib2
import re
h = urllib2.urlopen('http://www.proxy360.cn/Region/Brazil')
html = h.read()
parser_port = '<span.*>\s*([0-9]){2,}\s*</span>'
p = re.compile(parser_port)
list_port = p.findall(html)
print list_port但是我得到了这个输出:
['8', '8', '0', '0', '0', '8', '8', '0', '0', '8', '8', '8', '8', '8', '8', '8', '8', '0']例如,我需要它来匹配8080。
发布于 2015-11-19 12:18:09
您正在重复组([0-9]){2,}。它用最后一个值覆盖。
相反,在组内重复子模式:
<span[^>]*>\s*([0-9]{2,})\s*</\s*span>码
parser_port = '<span[^>]*>\s*([0-9]{2,})\s*</\s*span>'
p = re.compile(parser_port)
list_port = p.findall(html)发布于 2015-11-19 12:10:43
如果你想把端口从页面上拉下来。
parser_port = '<span.*>\s*([0-9]{2,})+\s*</span>'您需要至少两个长度( {2,} )的一个或多个字符(+符号)。但是还不清楚用例是什么。
https://stackoverflow.com/questions/33803471
复制相似问题