【发布时间】:2016-02-20 15:01:03
【问题描述】:
我是 python 正则表达式的初学者
目标test.php代码:
<html>
<head></head>
<body>
<a href="www.google.com">josn2051@yahoo.com.tw</a>
<div>john@yahoo.com.tw</div>
testtest321@gmail.com
chorm3636@test.test.test.com
</body>
</html>
这是我的代码:
import requests,re
email_pattern = re.compile('([\w\-\.]+@(\w[\w\-]+\.)+[\w\-]+)')
res = requests.get("http://127.0.0.1/test.php")
a = email_pattern.findall(res.text)
print a
结果:
[(u'josn2051@yahoo.com.tw', u'com.'), (u'john@yahoo.com.tw', u'com.'), (u'asdfFGw@gmail.com', u'gmail.'), (u'chorm3636@test.test.test.com', 你'测试')]
但我想要这样的结果:
[josn2051@yahoo.com.us, john@yahoo.com.us, testtest321@gmail.com, chorm3636@test.test.test.com]
我的模式或代码有什么问题?
为什么结果是多个列表包含额外的com、gmail、test?
谢谢你解决了我的疑惑!
【问题讨论】:
-
因为抓包,使用
'([\w\-\.]+@(?:\w[\w\-]+\.)+[\w\-]+)' -
所以我的模式似乎有不必要的括号?
-
彼得伍德,谢谢!链接很有用