Python‎ > ‎requests‎ > ‎

requests.get


Definition
The method returns with the get request from the server.

Syntax
requests.get(URL, parameters)

Parameters=string, number, list, dictionary

Examples
The function calculated string 'turtle' in webpage in django.
STEPS for creating virtual environment, startproject, startapps, migration, & running server.
  • virtualenv [virtual environment name]
  • cd  [virtual environment name]
  • .\Scripts\activate
  • .\Scripts\deactivate
  • pip install django
  • python .\Scripts\django-admin.py startproject [project name]
  • cd [project name]
  • python manage.py runserver
  • python manage.py startapp [name of app]
  • python manage.py migrate
  • python manage.py runserver
 After virtual environment, startproject, and startapp, the files structure look like this:


    In setting.py, put following intended app name, here it is newrequests.
    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles','newrequests',
    )

    In urls.py, put following code: 
    from django.conf.urls import include, url
    from django.contrib import admin

    from newrequests.views import request1

    urlpatterns = [
        url(r'^newrequests/$', request1),
    ]

    In views.py, put following code:
    from django.http import HttpResponse
    import requests

    def request1(request):
        r=requests.get('http://www.pythonlake.com/Python/turtle')
        c=r.text
        ct=c.count('turtle')
        return HttpResponse(ct)
     
    Running the server at localhost:8000 through following command in CMD:
     python manage.py runserver



    The pyramid returns with the requests after finding the number of 'a' in www.pythonlake.com.
    from wsgiref.simple_server import make_server
    from pyramid.response import Response
    import requests

    def find(request):
        r=requests.get('http://www.pythonlake.com')
        text=r.text
        f=text.find('a')
        f=str(f)
        response = Response()
        response.text=f
        return response
        
    if __name__ == '__main__':
        config = Configurator()
        config.add_route('find', '/')
        config.add_view(find, route_name='find')
        app = config.make_wsgi_app()
        server = make_server('0.0.0.0', 6500, app)
        server.serve_forever()






    The for loop returns the headers values.
     >>> import requests
    >>> r=requests.get('http://www.pythonlake.com/aboutus')
    >>> for s in r.headers:
    ...     print(s)
    ... 
    x-xss-protection
    transfer-encoding
    x-content-type-options
    vary
    expires
    content-type
    server
    date
    cache-control
    accept-ranges
    last-modified
    x-robots-tag
    alternate-protocol


    The method verify can be used to verify the whether the HTTP certification has expired o not, if the response return error certification is expired.
     >>> import requests
    >>> v=requests.get('http://www.gmail.com', verify=True)
    >>> v
    <Response [200]>

    The request request is subject to the exception of ConnectionError in finding the turtle parameters in the www.pythonlake.com homepage.
    >>> import requests
     >>> try:
    ...     r=requests.get('http://www.pythonlake.com',params='www.pythonlake.com')
    ...     t=r.text
    ...     t.find('turtle')
    ... except:
    ...     print(e)
    ...     
    103986
    >>> 


    The code return with the exception of HTTPError because URL error.
     >>> try:
    ...     r=requests.get('http://www.pythonlake.c/turtle',params='turtle')
    ...     t=r.text
    ...     print(t)
    ... except:
    ...     print(e)
    ...     
    <class 'requests.exceptions.HTTPError'>


    The code gets http library of  www.pythonlake.com and store in the variable r and str.count method is used to count the strings.

    >>> import requests
    >>> r=requests.get('http://www.pythonlake.com')
    >>> t=r.text
    >>> t.count('www.pythonlake.com')
    721 

    The headers returns with the server response to get request and if statement is used to open the webbrowser condition to dict.keys.
     >>>import requests
    >>> re=requests.get('http://www.pythonlake.com', params='Pythonlake')
    >>> re.headers
    {'x-content-type-options': 'nosniff', 'server': 'GSE', 'x-robots-tag': 'noarchive', 'x-xss-protection': '1; mode=block', 'date': 'Mon, 27 Apr 2015 13:27:21 GMT', 'accept-ranges': 'none', 'vary': 'Accept-Encoding', 'cache-control': 'public, max-age=5', 'transfer-encoding': 'chunked', 'content-type': 'text/html; charset=utf-8', 'last-modified': 'Mon, 27 Apr 2015 13:16:45 GMT', 'expires': 'Mon, 27 Apr 2015 13:27:26 GMT', 'alternate-protocol': '80:quic,p=1,80:quic,p=1'}
    >>> 
    >>> h.keys
    <bound method CaseInsensitiveDict.keys of {'x-content-type-options': 'nosniff', 'server': 'GSE', 'x-robots-tag': 'noarchive', 'x-xss-protection': '1; mode=block', 'date': 'Mon, 27 Apr 2015 13:27:21 GMT', 'accept-ranges': 'none', 'vary': 'Accept-Encoding', 'cache-control': 'public, max-age=5', 'transfer-encoding': 'chunked', 'content-type': 'text/html; charset=utf-8', 'last-modified': 'Mon, 27 Apr 2015 13:16:45 GMT', 'expires': 'Mon, 27 Apr 2015 13:27:26 GMT', 'alternate-protocol': '80:quic,p=1,80:quic,p=1'}>
    >>> if 'server' in h.keys():
    ...     >>> webbrowser.open_new('www.pythonlake.com')
    True
    >>>



     The requests loaded content from google.com with text "Adil Waseem" and BeautifulSoup4 searched all hyperlink in the re.content.
     import requests
    from bs4 import BeautifulSoup

    payload=payload = {'q': 'Adil Waseem'}
    re=requests.get("https://www.google.com/", params=payload)
    soup=BeautifulSoup(re.content,"lxml")
    print(soup.find_all("a"))


     ========= RESTART: C:/xampp/htdocs/PhpProject1/testeditor/convert.py =========
    [<a class="gb1" href="https://www.google.com/imghp?hl=en&amp;tab=wi">Images</a>, <a class="gb1" href="https://maps.google.com/maps?hl=en&amp;tab=wl">Maps</a>, <a class="gb1" href="https://play.google.com/?hl=en&amp;tab=w8">Play</a>, <a class="gb1" href="https://www.youtube.com/?gl=US&amp;tab=w1">YouTube</a>, <a class="gb1" href="https://news.google.com/nwshp?hl=en&amp;tab=wn">News</a>, <a class="gb1" href="https://mail.google.com/mail/?tab=wm">Gmail</a>, <a class="gb1" href="https://drive.google.com/?tab=wo">Drive</a>, <a class="gb1" href="https://www.google.com/intl/en/options/" style="text-decoration:none"><u>More</u> »</a>, <a class="gb4" href="http://www.google.com/history/optout?hl=en">Web History</a>, <a class="gb4" href="/preferences?hl=en">Settings</a>, <a class="gb4" href="https://accounts.google.com/ServiceLogin?hl=en&amp;passive=true&amp;continue=https://www.google.com/%3Fq%3DAdil%2BWaseem" id="gb_70" target="_top">Sign in</a>, <a href="/advanced_search?hl=en&amp;authuser=0">Advanced search</a>, <a href="/language_tools?hl=en&amp;authuser=0">Language tools</a>, <a href="/intl/en/ads/">Advertising Programs</a>, <a href="/services/">Business Solutions</a>, <a href="https://plus.google.com/116899029375914044550" rel="publisher">+Google</a>, <a href="/intl/en/about.html">About Google</a>, <a href="/intl/en/policies/privacy/">Privacy</a>, <a href="/intl/en/policies/terms/">Terms</a>]
    >>> 

    Comments