Whenever it comes to programming a web service, you will require a solid database backend. In the past, programmers used to write raw SQL statements, pass them to the database engine and parse the returned results as a normal array of records. Nowadays, programmers can write Object-Relational Mapping (ORM) programs to remove the necessity of writing tedious and error-prone raw SQL statements.
Most programming language platforms are object oriented. ORM is a programming technique for converting data between incompatible type systems in object-oriented programming languages. Data in RDBMS (Relational Database Management System) servers on the other hand is stored as tables. Object relation mapping is a technique of mapping object parameters to the underlying RDBMS table structure. An ORM API provides methods to perform create, read, update, delete (CRUD) operations without having to write raw SQL statements. So basically, an ORM takes care of these issues while you can focus on programming the logics of the system.
In order to run the python script with SQLAlchemy codes in it, you need to install flask-sqlalchemy extension first for that run the command :
1 |
pip install flask-sqlalchemy |
For using functions from SQLAlchemy in your program, you need to import it first using the statement :
1 |
from flask_sqlalchemy import SQLAlchemy |
Now you can create your Flask application and make surem you set the URI for the database which is to be used in your program.
1 2 |
app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///persons.sqlite3' |
Now, you need to create an object of SQLAlchemy class with the application object provided as parameter. The object provides helper functions for ORM operations and also provides a Model class using which user-defined models for database is declared. In the following code snippet a persons model is created:
1 2 3 4 5 6 7 8 |
db = SQLAlchemy(app) class persons(db.Model): id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(100)) phone = db.Column(db.String(10)) def __init__(self, name, phone): self.name = name self.phone = phone |
To create or use the database that we specified in the URI run the create_all() method.
1 |
db.create_all() |
To add an object data to the database we can use the following code:
1 |
db.session.add(model object) |
For deleting just replace the add function with delete function and if you need to retrieve the records of the table use:
1 |
model.query.all() |
You can also apply filters while retrieving records. For eg:
1 |
model.query.filter_by(name = ‘abc’).all() |
will return the set of table rows whose city attribute is equal to ‘Mumbai’.
Now, let’s see an entire program based on what we’ve learned so far:
First let’s setup an html page to display our database contents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<!DOCTYPE html> <html lang = "en"> <head></head> <body> <h3> <a>SQLAlchemy TEST</a> </h3> <table> <thead> <tr> <th>Name</th> <th>Phone</th> </tr> </thead> <tbody> {% for person in persons %} <tr> <td>{{ person.name }}</td> <td>{{ person.phone }}</td> </tr> {% endfor %} </tbody> </table> </body> </html> |
We’ve set an HTML page with a heading SQLAlchemy Test and it has a table with two columns to display a persons name and phone number. Now we need to code a python script to do our job.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from flask import Flask, request, flash, url_for, redirect, render_template from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///persons.sqlite3' app.config['SECRET_KEY'] = "random string" db = SQLAlchemy(app) class persons (db.Model): id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(100)) phone = db.Column(db.String(10)) def __init__(self, name, phone): self.name = name self.phone = phone @app.route('/') def show_all(): person = persons("abc","9847068114") db.session.add(person) person = persons ("efg","9847068118") db.session.add(person) person = persons ("hijk","7012665458") db.session.add(person) person = persons ("lmno","7012898456") db.session.add(person) db.session.commit() return render_template('show_all.html', persons = persons.query.all() ) if __name__ == '__main__': db.create_all() app.run(debug = True) |
In the above python script, we create a model class for person which can hold values for a person’s name and phone number. We set the route of out app to the homepage of localhost. So after running the app, when you open the browser and visit your localhost homepage by typing 127.0.0.1:5000 in your browser’s address bar, you can see the html page we’ve setup. It will show all function finally returns the rendered template of show_all.html populated with the contents in the database.
Now, we can checkout how we add contents to the database. For that we need to create objects of our model class and provide values for its attributes i.e. name and phone of a person. Now we can add it to our database using SQLAlchemy_object.session.add(model object) function. After adding all the values make sure you commit the changes to the database in order to prevent data corruption. Now, we have our database ready with contents, we can display it on our html page using render template(‘html_file_name’, model = model.query.all()) function.
Save the python file and place the html file in a folder named templates. If no such folder exists create one. Now you can run the python script and visit 127.0.0.1:5000 in your browser to see your app running.
According to http://pajhome.org.uk/blog/10_reasons_to_love_sqlalchemy.html here are the top 10 reasons to love SQLAlchemy
Scrapy is an open source and collaborative framework for extracting the data you need from websites in a fast, simple, yet extensible way.
Web scraping is a computer software technique of extracting information from websites. This technique mostly focuses on the transformation of unstructured data (HTML format) on the web into structured data (database or spreadsheet).
In python, web scraping can be done using scrapy.
Installation first.
You can easily install using pip. For other installation option, click here. Type the following to your command prompt.
1 |
pip install scrapy |
Now, let’s get our hand on some coding.
Let’s start off by creating a scrapy project. Enter the directory of your choice and type in the following.
1 |
scrapy startproject tutorial |
Something like this prints out for you.
1 2 3 |
New Scrapy project 'tutorial', using template directory '/usr/local/lib/python2.7/dist-packages/scrapy/templates/project', created in: /home/enfa/Desktop/BS4/tutorial |
You can start your first spider with:
1 2 3 |
cd tutorial scrapy genspider example example.com |
This will create a directory tutorial with the following contents.
tutorial/
scrapy.cfg # deploy configuration file
tutorial/ # project’s Python module, you’ll import your code from here
__init__.py
items.py # project items definition file
pipelines.py # project pipelines file
settings.py # project settings file
spiders/ # a directory where you’ll later put your spiders
__init__.py
Now let’s create a spider, but what are spiders?
Spiders are classes that you define and that Scrapy uses to scrape information from a website (or a group of websites). They must have a subclass scrapy. Spider and define the initial requests to make, optionally how to follow links in the pages, and how to parse the downloaded page content to extract data.
We will be using examples from the official doc.
So save the following code in a file named quotes_spider.py under the tutorial/spiders directory in your project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import scrapy class QuotesSpider(scrapy.Spider): name = "quotes" def start_requests(self): urls = [ 'http://quotes.toscrape.com/page/1/', 'http://quotes.toscrape.com/page/2/', ] for url in urls: yield scrapy.Request(url=url, callback=self.parse) def parse(self, response): page = response.url.split("/")[-2] filename = 'quotes-%s.html' % page with open(filename, 'wb') as f: f.write(response.body) self.log('Saved file %s' % filename) |
As you can see, our Spider subclasses scrapy.Spider
Let’s see wha teach of the attributes and methods mean.
The parse() method usually parses the response, extracting the scraped data as dicts and also finding new URLs to follow and creating new requests (Request) from them.
Now let’s run our spider.
Go to the top level directory and type in the following in your cmd.
1 |
scrapy crawl quotes |
This command runs the spider with name quotes that we’ve just added, that will send some requests for the quotes.toscrape.com domain. You will get an output similar to this:
… (omitted for brevity)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
2017-7-1 21:24:05 [scrapy.core.engine] INFO: Spider opened 2017-7-1 21:24:05 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min) 2017-7-1 21:24:05 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023 2017-7-1 21:24:05 [scrapy.core.engine] DEBUG: Crawled (404) <GET http://quotes.toscrape.com/robots.txt> (referer: None) 2017-7-1 21:24:05 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://quotes.toscrape.com/page/1/> (referer: None) 2017-7-1 21:24:05 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://quotes.toscrape.com/page/2/> (referer: None) 2017-7-1 21:24:05 [quotes] DEBUG: Saved file quotes-1.html 2017-7-1 21:24:05 [quotes] DEBUG: Saved file quotes-2.html 2017-7-1 21:24:05 [scrapy.core.engine] INFO: Closing spider (finished) |
…
Source:https://doc.scrapy.org/en/latest/intro/tutorial.html
Note:
Two new files have been created in the directory you were at. quotes-1.html and quotes-2.html, with the content for the respective URLs, as our parse method instructs.
Beautiful! Isn’t it?
To learn to play with scrapy, check out
https://doc.scrapy.org/en/latest/intro/tutorial.html
www.tutorialspoint.com/scrapy/
Requests is an elegant and simple Apache2 licensed HTTP library for Python. It is designed to be used by humans to interact with the language. This means you don’t have to manually add query strings to URLs, or form-encode your POST data.
Requests will allow you to send HTTP/1.1 requests using Python. With it, you can add content like headers, form data, multipart files, and parameters via simple Python libraries. It also allows you to access the response data of Python in the same way.
Here, I am installing using pip. For more options, visit.
1 |
pip install requests |
First lets import requests
1 |
import requests |
Here, i am using a webpage within my server which open to a page like this.
Let’s create a Response Object r
1 |
r = requests.get('http://localhost/hello.php') |
Let’s check the current status of the site
1 2 3 |
r.status_code #200 |
1 2 3 |
r.status_code == requests.codes.ok #True |
1 2 3 |
requests.codes['temporary_redirect'] #307 |
1 2 3 |
requests.codes.teapot #418 |
Let’s get the content of the site
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
print r.text """ <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <h2>PHP Form Validation Example</h2> <p><span class="error">* required field.</span></p> <form method="post" action="/hello.php"> Name: <input type="text" name="name" value=""> <span class="error">* </span> <br><br> E-mail: <input type="text" name="email" value=""> <span class="error">* </span> <br><br> Website: <input type="text" name="website" value=""> <span class="error"></span> <br><br> Comment: <textarea name="comment" rows="5" cols="40"></textarea> <br><br> Gender: <input type="radio" name="gender" value="female">Female <input type="radio" name="gender" value="male">Male <span class="error">* </span> <br><br> <input type="submit" name="submit" value="Submit"> </form> <h2>Your Input:</h2><br><br><br><br> </body> </html> """ |
To know what encoding it uses,
1 2 3 |
r.encoding #'UTF-8' |
Let’s change that to say,
1 |
r.encoding = 'ISO-8859-1' |
You might want to do this in any situation where you can apply special logic to work out what the encoding of the content will be. For example, HTTP and XML have the ability to specify their encoding in their body. In situations like this, you should use r.content to find the encoding, and then set r.encoding. This will let you use r.text with the correct encoding.
Requests will also use custom encoding in the event. If you have created your own encoding and registered it with the codecs module, you can simply use the codec name as the value of r.encoding and Requests will handle the decoding for you.
There’s also a builtin JSON decoder, in case you’re dealing with JSON data:
1 |
r.json() |
With every request you send to a HTTP server, the server will send you some additional data. You can get extract data from an HTTP response using:
1 2 3 |
print r.headers """{'Content-Length': '393', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'Keep-Alive': 'timeout=5, max=100', 'Server': 'Apache/2.4.10 (Debian)', 'Connection': 'Keep-Alive', 'Date': 'Fri, 07 Jul 2017 14:09:24 GMT', 'Content-Type': 'text/html; charset=UTF-8'}""" |
This is the list of features from the requests site:
An awesome lotta features!Right?
To know more,
http://docs.python-requests.org/en/master/
http://docs.python-requests.org/en/latest/api/
http://pypi.python.org/pypi/requests
http://docs.python-requests.org/en/latest/user/quickstart/
http://isbullsh.it/2012/06/Rest-api-in-python/#requests
http://docs.python-requests.org/en/master/user/quickstart/
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Python Django takes care of the repetitive work for you so that you don’t have to reinvent the wheel all over again.
You can install Django using
1 |
$pip install django |
For other ways of installation, visit this.
Since this is the first time we are using Django there is some initial setup to do. We need to auto generate some code that establishes the Django project.
From the command line,
1 |
$ django-admin startproject mysite |
This will create a folder ‘mysite ‘ in your current directory. Do avoid python names of different functionalities as names for the folder. Ex. Django and test are not good choices since they create conflict on calling.
Also, with Django you do not put your code under your web server’s root directory. We always place it outside.
Let’s look at what startproject created:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
These files are:
Let’s verify whether our Django works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
root@localhost:/home/enfa/Desktop/GlobalSQA/mysite# python manage.py runserver Performing system checks... System check identified no issues (0 silenced). You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. July 07, 2017 - 15:28:53 Django version 1.11.3, using settings 'mysite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. |
You can safely ignore the warning for now.
Yipppe! We have started the Django server! Let’s go see how it looks like.
Note This is only for studying. When it comes to production setting, use Apache servers or similar.
By default the port is 8000. But you can change it using the command line. Say you want to change it to 8080
1 |
$ python manage.py runserver 8080 |
Full docs for the development server can be found in the runserver reference
To know how to edit and configure your webpages, check out https://docs.djangoproject.com/en/1.11/intro/tutorial01/
For more details visit,
https://www.djangoproject.com/
https://docs.djangoproject.com/en/1.11/#index-first-steps
https://www.djangoproject.com/start/
https://docs.djangoproject.com/en/1.11/
https://tutorial.djangogirls.org/en/django_start_project/