• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python django如何使用正则搜索页面上的email地址

python 水墨上仙 1387次浏览

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:
      # download the content of the page,http://www.75271.com
      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 django如何使用正则搜索页面上的email地址
喜欢 (0)
加载中……