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)文章
Windows 下更改 jupyterlab 默認(rèn)啟動位置的教程詳解
這篇文章主要介紹了Windows 下更改 jupyterlab 默認(rèn)啟動位置,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
解決pycharm不能自動補(bǔ)全第三方庫的函數(shù)和屬性問題
python des,aes,rsa加解密的實現(xiàn)
Python小程序 控制鼠標(biāo)循環(huán)點擊代碼實例

