python中django框架通過正則搜索頁面上email地址的方法
本文實例講述了python中django框架通過正則搜索頁面上email地址的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
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)
PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
python將數(shù)據(jù)插入數(shù)據(jù)庫的代碼分享
在本篇文章里小編給大家整理的是關(guān)于python將數(shù)據(jù)插入數(shù)據(jù)庫的代碼內(nèi)容,有興趣的朋友們可以參考下。2020-08-08
Linux下使用python調(diào)用top命令獲得CPU利用率
這篇文章主要介紹了Linux下使用python調(diào)用top命令獲得CPU利用率,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-03-03
Pytorch建模過程中的DataLoader與Dataset示例詳解
這篇文章主要介紹了Pytorch建模過程中的DataLoader與Dataset,同時PyTorch針對不同的專業(yè)領(lǐng)域,也提供有不同的模塊,例如?TorchText,?TorchVision,?TorchAudio,這些模塊中也都包含一些真實數(shù)據(jù)集示例,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-01-01

