我想知道是否有一种方法可以用特定的结构化格式将html保存在我的文件中。现在,此脚本的输出只是将字母和数字捆绑在一起。有没有一种结构上的方法?例如: 111.111.111.11:111 222.222.222.22:22 (IP格式)
如有任何帮助,我们不胜感激!
import urllib.request
import re
ans = True
while ans:
print("""
- Menu Selection -
1. Automatic
2. Automatic w/Checker
3. Manual
4. Add to list
5. Exit
""")
ans = input('Select Option : ')
if ans =="1":
try :
with urllib.request.urlopen('http://www.mywebsite.net') as response:
html = response.read()
html = str(html)
html = re.sub(r'([a-z][A-Z])', '', html)
f = open('text.txt','a')
f.write(html)
f.close()
print('Data(1) saved.')
ans = True
except :
print('Error on first fetch.') 发布于 2016-05-23 05:27:44
根据问题-
如果样本输入是-
fdsfdsfdsf123.123.123.123:123fdds125.125.125.125:125fdsfdfdsfdsfsdf Input
输出- 123.123.123.123:123 (换行符) 125.125.125.125:125
如果html是输入字符串-
filtered_alpha = re.sub('[^0-9\.:]','\n', html)
multiple_ips = filter(None, filtered_alpha.split("\n"))
print "\n".join(multiple_ips)这将为您提供预期的输出。
如果你正在专门寻找ip_addresses,你可以参考@MarkByers here的帖子,他提到了-
ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', html)
https://stackoverflow.com/questions/37379627
复制相似问题