使用正则表达式在网页中查找电子邮件地址

Kyu*_*ark 2 python regex search webpage

我是Python的初级学生.这是我必须从网页中找到电子邮件地址实例的代码.

    page = urllib.request.urlopen("http://website/category")
    reg_ex = re.compile(r'[-a-z0-9._]+@([-a-z0-9]+)(\.[-a-z0-9]+)+', re.IGNORECASE
    m = reg_ex.search_all(page)
    m.group()
Run Code Online (Sandbox Code Playgroud)

当我运行它时,Python模块说有一个无效的语法,它就在线上:

    m = reg_ex.search_all(page)
Run Code Online (Sandbox Code Playgroud)

有人会告诉我为什么它无效吗?

Tom*_*OKe 5

考虑另一种选择:

## Suppose we have a text with many email addresses
str = 'purple alice@google.com, blah monkey bob@abc.com blah dishwasher'

## Here re.findall() returns a list of all the found email strings
emails = re.findall(r'[\w\.-]+@[\w\.-]+', str) 
    ## ['alice@google.com', 'bob@abc.com']    
for email in emails:
    # do something with each found email string
    print email
Run Code Online (Sandbox Code Playgroud)

资料来源:https://developers.google.com/edu/python/regular-expressions