实现一个简单的邮箱地址爬虫(python)
我经常收到关于email爬虫的问题。有迹象表明那些想从网页上抓取联系方式的人对这个问题很感兴趣。在这篇文章里,我想演示一下如何使用python实现一个简单的邮箱爬虫。这个爬虫很简单,但从这个例子中你可以学到许多东西(尤其是当你想做一个新虫的时候)。
我特意简化了代码,尽可能的把主要思路表达清楚。这样你就可以在需要的时候加上自己的功能。虽然很简单,但完整的实现从网上抓取email地址的功能。注意,本文的代码是使用python3写的。
好。让我们逐步深入吧。我一点一点的实现,并加上注释。最后再把完整的代码贴出来。
首先引入所有必要的库。在这个例子中,我们使用的BeautifulSoup 和 Requests 是第三方库,urllib, collections 和 re 是内置库。
BeaufulSoup可以使检索Html文档更简便,Requests让执行web请求更容易。
from bs4 import BeautifulSoup
import requests
import requests.exceptions
from urllib.parse import urlsplit
from collections import deque
import re
下面我定义了一个列表,用于存放要抓取的网页地址,比如http://www.huazeming.com/ ,当然你也可以找有明显email地址的网页作为地址,数量不限。虽然这个集合应该是个列表(在python中),但我选择了 deque 这个类型,因为这个更符合我们的需要。
# a queue of urls to be crawled
new_urls = deque(['http://www.themoscowtimes.com/contact_us/'])
接下来,我们需要把处理过的url存起来,以避免重复处理。我选择set类型,因为这个集合可以保证元素值不重复。
# a set of urls that we have already crawled
processed_urls = set()
定义一个email集合,用于存储收集到地址:
# a set of crawled emails
emails = set()
让我们开始抓取吧!我们有一个循环,不断取出队列的地址进行处理,直到队列里没有地址为止。取出地址后,我们立即把这个地址加到已处理的地址列表中,以免将来忘记。
# process urls one by one until we exhaust the queue
while len(new_urls):
# move next url from the queue to the set of processed urls
url = new_urls.popleft()
processed_urls.add(url)
然后我们需要从当前地址中提取出根地址,这样当我们从文档中找到相对地址时,我们就可以把它转换成绝对地址。
# extract base url and path to resolve relative links
parts = urlsplit(url)
base_url = "{0.scheme}://{0.netloc}".format(parts)
path = url[:url.rfind('/')+1] if '/' in parts.path else url
下面我们从网上获取页面内容,如果遇到错误,就跳过继续处理下一个网页。
# get url's content
print("Processing %s" % url)
try:
response = requests.get(url)
except (requests.exceptions.MissingSchema, requests.exceptions.ConnectionError):
# ignore pages with errors
continue
当我们得到网页内容后,我们找到内容里所有email地址,把其添加到列表里。我们使用正则表达式提取email地址:
# extract all email addresses and add them into the resulting set
new_emails = set(re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", response.text, re.I))
emails.update(new_emails)
在我们提取完当前网页内容的email地址后,我们找到当前网页中的其他网页地址,并将其添加到带处理的地址队列里。这里我们使用BeautifulSoup库来分析网页html。
# create a beutiful soup for the html document
soup = BeautifulSoup(response.text)
这个库的find_all方法可以根据html标签名来抽取元素。
# find and process all the anchors in the document
for anchor in soup.find_all("a"):
但网页总的有些a标签可能不包含url地址,这个我们需要考虑到。
# extract link url from the anchor
link = anchor.attrs["href"] if "href" in anchor.attrs else ''
如果这个地址以斜线开头,那么我们把它当做相对地址,然后给他加上必要的根地址:
# add base url to relative links
if link.startswith('/'):
link = base_url + link
到此我们得到了一个有效地址(以http开头),如果我们的地址队列没有,而且之前也没有处理过,那我们就把这个地址加入地址队列里:
# add the new url to the queue if it's of HTTP protocol, not enqueued and not processed yet
if link.startswith('http') and not link in new_urls and not link in processed_urls:
new_urls.append(link)
好,就是这样。以下是完整代码:
from bs4 import BeautifulSoup
import requests
import requests.exceptions
from urllib.parse import urlsplit
from collections import deque
import re # a queue of urls to be crawled
new_urls = deque(['http://www.themoscowtimes.com/contact_us/index.php']) # a set of urls that we have already crawled
processed_urls = set() # a set of crawled emails
emails = set() # process urls one by one until we exhaust the queue
while len(new_urls): # move next url from the queue to the set of processed urls
url = new_urls.popleft()
processed_urls.add(url) # extract base url to resolve relative links
parts = urlsplit(url)
base_url = "{0.scheme}://{0.netloc}".format(parts)
path = url[:url.rfind('/')+1] if '/' in parts.path else url # get url's content
print("Processing %s" % url)
try:
response = requests.get(url)
except (requests.exceptions.MissingSchema, requests.exceptions.ConnectionError):
# ignore pages with errors
continue # extract all email addresses and add them into the resulting set
new_emails = set(re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", response.text, re.I))
emails.update(new_emails) # create a beutiful soup for the html document
soup = BeautifulSoup(response.text) # find and process all the anchors in the document
for anchor in soup.find_all("a"):
# extract link url from the anchor
link = anchor.attrs["href"] if "href" in anchor.attrs else ''
# resolve relative links
if link.startswith('/'):
link = base_url + link
elif not link.startswith('http'):
link = path + link
# add the new url to the queue if it was not enqueued nor processed yet
if not link in new_urls and not link in processed_urls:
new_urls.append(link)
这个爬虫比较简单,省去了一些功能(比如把邮箱地址保存到文件中),但提供了编写邮箱爬虫的一些基本原则。你可以尝试对这个程序进行改进。
当然,如果你有任何问题和建议,欢迎指正!
英文原文:A Simple Email Crawler in Python
实现一个简单的邮箱地址爬虫(python)的更多相关文章
- 用一个简单的例子来理解python高阶函数
============================ 用一个简单的例子来理解python高阶函数 ============================ 最近在用mailx发送邮件, 写法大致如 ...
- 一个简单的开源PHP爬虫框架『Phpfetcher』
这篇文章首发在吹水小镇:http://blog.reetsee.com/archives/366 要在手机或者电脑看到更好的图片或代码欢迎到博文原地址.也欢迎到博文原地址批评指正. 转载请注明: 吹水 ...
- 一个简单、易用的Python命令行(terminal)进度条库
eprogress 是一个简单.易用的基于Python3的命令行(terminal)进度条库,可以自由选择使用单行显示.多行显示进度条或转圈加载方式,也可以混合使用. 示例 单行进度条 多行进度条 圆 ...
- [Python Study Notes]一个简单的区块链结构(python 2.7)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- 一个简单的app自动登录Python脚本案例
- 一个简单的go语言爬虫
package main import ( "bufio" "fmt" "golang.org/x/net/html/charset" &q ...
- Python学习手册之正则表达式示例--邮箱地址提取
在上一篇文章中,我们介绍了 Python 的捕获组和特殊匹配字符串,现在我们介绍 Python 的正则表达式使用示例.查看上一篇文章请点击:https://www.cnblogs.com/dustma ...
- 使用James搭建一个自己的邮箱服务器
---第一天开发--- 下载Apache James 3.0邮箱服务器,解压到响应的目录 可以看到目录结构: H:\code\JavaCode\James\apache-james-3.0-beta4 ...
- 一个简单的python爬虫,爬取知乎
一个简单的python爬虫,爬取知乎 主要实现 爬取一个收藏夹 里 所有问题答案下的 图片 文字信息暂未收录,可自行实现,比图片更简单 具体代码里有详细注释,请自行阅读 项目源码: # -*- cod ...
随机推荐
- Redis事务和分布式锁
Redis事务 Redis中的事务(transaction)是一组命令的集合.事务同命令一样都是Redis最小的执行单位,一个事务中的命令要么都执行,要么都不执行.Redis事务的实现需要用到 MUL ...
- wx.button
wx.Button A button is a control that contains a text string, and is one of the most common elements ...
- 合理的keyword密度散布与黑帽SEO之躲藏文本
合理的keyword密度散布与黑帽SEO之躲藏文本 咱们都晓得.关于baidu的keyword排行有一个非常重要的条件即是keyword密度.在咱们的了解中keyword的密度在2%-8%这个规模之内 ...
- UNIX网络编程--读书笔记
会集中这段时间写UNIX网络编程这本书的读书笔记,准备读三本,这一系类的文章会不断更新,一直会持续一个月多,每篇的前半部分是书中讲述的内容,每篇文章的后半部分是自己的心得体会,文章中的红色内容是很重要 ...
- HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个带边权的图.对于每一个询问(S , ...
- SQL Server数据库--》top关键字,order by排序,distinct去除重复记录,sql聚合函数,模糊查询,通配符,空值处理。。。。
top关键字:写在select后面 字段的前面 比如你要显示查询的前5条记录,如下所示: select top 5 * from Student 一般情况下,top是和order by连用的 orde ...
- @Html.ValidationSummary()的使用
@Html.ValidationSummary()用于返回表单在后台验证的结果. 如, 当后台if (ModelState.IsValid)失败后,错误信息就会显示到 @Html.Validation ...
- 通过OCI 处理 Oracle 10g 中处理Clob大字段写入
Oracle数据库中, 通过存储过程写入Clob字段 , 当数据大于4k时, 报错 ORA-01460: 转换请求无法实施或不合理 经过排查, 数据Bind方式不对, 不能采用字符串的Bind方式 原 ...
- 在Windows的CMD中如何设置支持UTF8编码
这个问题很多人开始都会不知道,当然包括曾经的我,当用到的时候,只好求助于伟大的股沟和度娘了.网上有设置的方法,但说明确不够详细系统,说设置字体为:Lucida Console.问题是,在默认方式下,只 ...
- linux使用工具记录
linux工具查询手册: http://linuxtools-rst.readthedocs.io/zh_CN/latest/index.html