python中django框架通过正则搜索页面上email地址的方法


Posted in Python onMarch 21, 2015

本文实例讲述了python中django框架通过正则搜索页面上email地址的方法。分享给大家供大家参考。具体实现方法如下:

import re
from django.shortcuts import render
from pattern.web import URL, DOM, abs, find_urls
def index(request):
 """
 find email addresses in requested url or contact page
 """
 error = ''
 emails = set()
 url_string = request.GET.get('url', '')
 EMAIL_REGEX = re.compile(r'[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}', re.IGNORECASE)
 # use absolute url or domain name
 url = URL(url_string) if url_string.startswith('http') else URL(domain=url_string,protocol='http')
 if url_string:
 try:
  dom = DOM(url.download(cached=True))
 except Exception, e:
  error = e
 else:
  contact_urls = { url.string }
  # search links of contact page
  for link in dom('a'):
  if re.search(r'contact|about', link.source, re.IGNORECASE):
   contact_urls.add(
   abs(link.attributes.get('href',''), base=url.redirect or url.string))
  for contact_url in contact_urls:
  # download contact page
  dom = DOM(URL(contact_url).download(cached=True))
  # search emails in the body of the page
  for line in dom('body')[0].content.split('\n'):
   found = EMAIL_REGEX.search(line)
   if found:
   emails.add(found.group())
 data = {
 'url': url_string,
 'emails': emails,
 'error': error,
 }
 return render(request, 'index.html', data)
Python 相关文章推荐
python引用DLL文件的方法
May 11 Python
Python使用Paramiko模块编写脚本进行远程服务器操作
May 05 Python
详解Python中的文件操作
Aug 28 Python
tensorflow实现softma识别MNIST
Mar 12 Python
pip install urllib2不能安装的解决方法
Jun 12 Python
python实现弹窗祝福效果
Apr 07 Python
PyTorch的深度学习入门之PyTorch安装和配置
Jun 27 Python
python绘制双Y轴折线图以及单Y轴双变量柱状图的实例
Jul 08 Python
Tensorflow实现神经网络拟合线性回归
Jul 19 Python
python实现将一维列表转换为多维列表(numpy+reshape)
Nov 29 Python
用pytorch的nn.Module构造简单全链接层实例
Jan 14 Python
关于Python错误重试方法总结
Jan 03 Python
Python去除列表中重复元素的方法
Mar 20 #Python
python在windows下实现ping操作并接收返回信息的方法
Mar 20 #Python
Python实现微信公众平台自定义菜单实例
Mar 20 #Python
python在windows和linux下获得本机本地ip地址方法小结
Mar 20 #Python
python使用三角迭代计算圆周率PI的方法
Mar 20 #Python
Cpy和Python的效率对比
Mar 20 #Python
Python通过PIL获取图片主要颜色并和颜色库进行对比的方法
Mar 19 #Python
You might like
php 破解防盗链图片函数
2008/12/09 PHP
windows下配置apache+php+mysql时出现问题的处理方法
2014/06/20 PHP
PHP加密解密字符串汇总
2015/04/26 PHP
PHP共享内存用法实例分析
2016/02/12 PHP
详解Yii2 之 生成 URL 的方法
2017/06/16 PHP
jQuery最佳实践完整篇
2011/08/20 Javascript
关于jQuery对象数据缓存Cache原理以及jQuery.data详解
2013/04/07 Javascript
js算法中的排序、数组去重详细概述
2013/10/14 Javascript
Javascript遍历table中的元素示例代码
2014/07/08 Javascript
jquery+php随机生成红包金额数量代码分享
2015/08/27 Javascript
JavaScript判断变量是否为数组的方法(Array)
2016/02/24 Javascript
动态JavaScript所造成一些你不知道的危害
2016/09/25 Javascript
extjs简介_动力节点Java学院整理
2017/07/17 Javascript
Node 自动化部署的方法
2017/10/17 Javascript
解决LayUI加上form.render()下拉框和单选以及复选框不出来的问题
2019/09/27 Javascript
原生js实现表格翻页和跳转
2020/09/29 Javascript
python通过ssh-powershell监控windows的方法
2015/06/02 Python
一篇文章入门Python生态系统(Python新手入门指导)
2015/12/11 Python
python实现聊天小程序
2018/03/13 Python
python requests指定出口ip的例子
2019/07/25 Python
python matplotlib 画dataframe的时间序列图实例
2019/11/20 Python
python list数据等间隔抽取并新建list存储的例子
2019/11/27 Python
python GUI库图形界面开发之PyQt5窗口类QMainWindow详细使用方法
2020/02/26 Python
Numpy 多维数据数组的实现
2020/06/18 Python
call在Python中改进数列的实例讲解
2020/12/09 Python
python中threading和queue库实现多线程编程
2021/02/06 Python
css3 图片圆形显示 如何CSS将正方形图片显示为圆形图片布局
2014/10/10 HTML / CSS
Ego Shoes官网:英国时髦鞋类品牌
2020/10/19 全球购物
巴西最大的巴士票务门户:Quero Passagem
2020/11/21 全球购物
商场总经理岗位职责
2014/02/03 职场文书
培训协议书范本
2014/04/22 职场文书
2014年幼儿园国庆主题活动方案
2014/09/16 职场文书
2016元旦晚会主持词
2015/07/01 职场文书
python调用ffmpeg命令行工具便捷操作视频示例实现过程
2021/11/01 Python
redis数据结构之压缩列表
2022/03/21 Redis
苹果可能正在打击不进行更新的 App
2022/04/24 数码科技