The Django framework in Python is the way to email addresses on a regular search page _python

Source: Internet
Author: User
Tags abs in python

The example in this article describes the way the Django framework in Python uses an email address on a regular search page. Share to everyone for your reference. The implementation method is as follows:

Import re from django.shortcuts import render from Pattern.web import URL, DOM, ABS, Find_urls def index (Request): "" Find emails addresses in requested URLs 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 E Lse:contact_urls = {url.string} # Search Links of contacts page for link in Dom (' a '): if Re.search (R ' Contact|abo Ut ', Link.source, re. IGNORECASE): Contact_urls.add (ABS (Link.attributes.get (' href ', '), Base=url.redirect or url.string)) for Contact_u  RL in Contact_urls: # download contacts page dom = Dom (URL (contact_url). Download (cached=true)) # Search emails in the The body of the page is in the DOM (' body ') [0].Content.split (' \ n '): Found = Email_regex.search (line) if Found:emails.add (Found.group ()) data = {' URL ': url_s Tring, ' emails ': emails, ' ERROR ': Error,} return render (Request, ' index.html ', data)

PS: Here again for you to provide 2 very convenient regular expression tools for your reference to use:

JavaScript Regular expression online test tool:
Http://tools.jb51.net/regex/javascript

Regular expression online generation tool:
Http://tools.jb51.net/regex/create_reg

I hope this article will help you with your Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.