电话号码和 E-mail 地址提取程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mygodhome/article/details/80512372

  1. 假设你有一个无聊的任务,要在一篇长的网页或文章中,找出所有电话号码和邮件地址。如果手动翻页,可能需要查找很长时间。如果有一个程序,可以在剪贴板的文本中查找电话号码和 E-mail 地址,那你就只要按一下 Ctrl-A 选择所有文本,按下 Ctrl-C 将它复制到剪贴板,然后运行你的程序。它会用找到的电话号码和 E-mail地址,替换掉剪贴板中的文本。

http://www.nostarch.com/contactus.htm。按下 Ctrl-A 选择该页的所有文本,按下 Ctrl-C将它复制到剪贴板。

#!/bin/python3
import pyperclip, re
phone=re.compile(r'''(
	(\d{3}|\(\d{3}\))? #area code
	(\s|-|\.) #separator
	(\d{3}) #first 3 digits
	(\s|-|\.) #separator
	(\d{4}) #last 4 digits
	)''',re.VERBOSE)
mail=re.compile(r'''(
	[a-zA-Z0-9._%+-]+ #username	
	@
	[a-zA-Z0-9.-]+ #domain name
	(\.[a-zA-Z]{2,4}) #dot-sth
	)''',re.VERBOSE)
text=str(pyperclip.paste())
matches=[]
for groups in phone.findall(text):
	phoneNum='-'.join([groups[1],groups[3],groups[5]])
	matches.append(phoneNum)
for groups in mail.findall(text):
	matches.append(groups[0])
#Copy results to clipboard
if len(matches)>0:
	pyperclip.copy('\n'.join(matches))
	print('Copy to clipboar')
	print('\n'.join(matches))
else:
	print('No phone or mail be found')

猜你喜欢

转载自blog.csdn.net/mygodhome/article/details/80512372