requests.get.text


Definition
Return with the text of the get request.

Syntax
request.get.text

Parameters
None

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 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 request counted the number string 'www.pythonlake.com' in the request.
    >>> import requests
    >>> r=requests.get('http://www.pythonlake.com')
    >>> t=r.text
    >>> t.count('www.pythonlake.com')
    721 

    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'>


    Comments