国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python中django框架通過正則搜索頁面上email地址的方法

2020-02-23 00:23:36
字體:
來源:轉載
供稿:網友

本文實例講述了python中django框架通過正則搜索頁面上email地址的方法。分享給大家供大家參考。具體實現方法如下:

import refrom django.shortcuts import renderfrom pattern.web import URL, DOM, abs, find_urlsdef 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款非常方便的正則表達式工具供大家參考使用:

JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript

正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 同德县| 义马市| 绥中县| 万盛区| 伊春市| 盖州市| 南涧| 天长市| 天津市| 扶风县| 阿拉善左旗| 阿拉善左旗| 边坝县| 昆明市| 文安县| 泾阳县| 喀喇沁旗| 土默特右旗| 丰城市| 潮州市| 葵青区| 沙雅县| 鹤山市| 肥东县| 阜阳市| 雅江县| 尼勒克县| 乌拉特中旗| 日照市| 仙游县| 乌兰浩特市| 都昌县| 屏山县| 临颍县| 泊头市| 楚雄市| 吉林市| 延安市| 措美县| 辉县市| 南陵县|