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

首頁 > 編程 > Python > 正文

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

2019-11-25 17:55:19
字體:
來源:轉載
供稿:網友

本文實例講述了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.VeVB.COm/regex/javascript

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东台市| 津市市| 江川县| 安平县| 永年县| 龙陵县| 会宁县| 沐川县| 容城县| 南昌市| 仙桃市| 灵川县| 六枝特区| 宿松县| 三明市| 赣州市| 抚松县| 桦甸市| 如东县| 临洮县| 黄山市| 安陆市| 深州市| 中方县| 和田县| 定州市| 江西省| 龙江县| 洪泽县| 邮箱| 肇州县| 油尖旺区| 洪洞县| 永善县| 洪江市| 汉沽区| 玛多县| 扎赉特旗| 祁东县| 临猗县| 宜黄县|