A way for the Django framework to search the email address on the page in Python

Source: Internet
Author: User
This example describes how 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 refrom django.shortcuts import renderfrom pattern.web import URL, DOM, ABS, FIND_URLSDEF Index (Request): "" "Find E mail addresses in requested URL or the 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_s Tring,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 to 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 Contac T_url in contact_urls: # download Contact page dom = dom (URL (contact_url). Download (cached=true)) # Search Emails In 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_stri Ng, ' emails ': emails, ' ERROR ': Error,} return render (Request, ' index.html ', data)

Hopefully this article will help you with 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.