【发布时间】:2011-02-22 20:52:57
【问题描述】:
我想使用 Python 从字符串(实际上是单行 HTML)中提取 IP 地址。
>>> s = "<html><head><title>Current IP Check</title></head><body>Current IP Address: 165.91.15.131</body></html>"
-- '165.91.15.131' 是我想要的!
我尝试使用正则表达式,但到目前为止我只能得到第一个数字。
>>> import re
>>> ip = re.findall( r'([0-9]+)(?:\.[0-9]+){3}', s )
>>> ip
['165']
但我对 reg-expression 没有牢牢把握;上面的代码是从网络上的其他地方找到并修改的。
【问题讨论】: