国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 屏南县| 肃北| 凯里市| 潼关县| 洞头县| 贵港市| 河北区| 古丈县| 鄂尔多斯市| 红安县| 慈溪市| 岑巩县| 永顺县| 蓬莱市| 应城市| 永年县| 合山市| 南涧| 商丘市| 搜索| 平潭县| 思茅市| 林西县| 海口市| 马山县| 陆川县| 济源市| 济南市| 南昌县| 巍山| 武汉市| 嘉峪关市| 积石山| 伊川县| 马公市| 七台河市| 崇阳县| 永春县| 宁武县| 海淀区| 六枝特区|