我正在尝试做的是提取HTML内容并找到一个我知道存在的特定字符串
import urllib.request
import re
response = urllib.request.urlopen('http://ipchicken.com/')
data = response.read()
portregex = re.compile('Remote[\s]+Port: [\d]+')
port = portregex.findall(str(data))
print(data)
print(port)
现在,以我为例,该网站包含Remote Port: 50880
,但我根本无法提出合适的正则表达式!谁能找到我的错误?
我在Windows上使用python 3.4
您错误地使用了方括号而不是圆括号:
portregex = re.compile(r'Remote\s+Port: (\d+)')
这样可以确保的结果re.findall()
仅包含匹配的数字(因为存在re.findall()
时仅返回捕获组的匹配项):
>>> s = "Foo Remote Port: 12345 Bar Remote Port: 54321"
>>> portregex.findall(s)
['12345', '54321']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句