吾八哥博客

您现在的位置是:首页 > 码农手记 > Python > 正文

Python

使用requests模块来匹配某个页面里的所有邮箱地址

吾八哥2018-01-19Python909

      采集邮箱地址也是经常被使用到的一个功能,吾八哥也练习一把使用Python来采集邮箱地址,基本思路为通过requests模块获取到页面内容,然后通过正则表达式去匹配邮箱字符,然后写入文件即可!

requests模块如果之前没有使用过,是需要提前安装的,安装方法也非常简单:pip install requests 然后等待就可以了!

       匹配邮箱的正则表达式为:r'[0-9a-zA-Z._]+@[0-9a-zA-Z._]+\.[0-9a-zA-Z._]+’

关于正则表达式,后面我们找个时间好好的学习下,后面会有非常大的用处的,吾八哥在使用其他开发语言的时候也是经常使用正则表达式。

       具体实现代码如下:

# Autor: 5bug
# WebSite: http://www.5bug.wang

import re
import requests

list_EMail = set()

def spiderEMail(url, filename):
    data = requests.get(url).text
    pattern = r'[0-9a-zA-Z._]+@[0-9a-zA-Z._]+\.[0-9a-zA-Z._]+'
    emails = re.compile(pattern).findall(data)
    with open(filename, 'w') as f:
        for email in emails:
            if email not in list_EMail:
                list_EMail.add(email)
                f.write(email + '\n')

spiderEMail('http://www.5bug.wang', 'email.txt')

print(list_EMail)

如果要做一个邮箱爬虫,则可以根据对应的行业关键字在搜索引擎或者其他的地方进行匹配,然后整站的抓取分析了。