** Prerequisites for the reader are to be familiar with Python programming.
Hello ..! Have you ever experienced difficulty in checking whether booking in a theatre have started or not? We go to the page and check for the theatre and repeat the process every five minutes..!! What if a program does that for you. 🙂 Yes, you read it correctly. In my first post I am going to explain my Python Program that does exactly this job.
The basic concept this program needs to do is to ping the vendor’s page (i.e BookMyShow in this case) frequently for every particular interval of time. If we closely inspect the Bookmyshow page which shows the cinemas we a similar pattern in which the shows are being displayed on the front end. By getting that part of the page’s HTML which shows the theaters solves the problem.
import the above mentioned libraries. If the library packages are not pre-installed in our system then you can use pip for installing them
As we don’t know the number of times program needs to ping the BookMyShow page, we use an infinite loop. So add any infinite loop.
Get the URL of the page that we have to parse which is similar to the below image.
Using the requests.get function with parameters as the URL of the movie’s HTML which shows theaters, get an object of the entire page.
page = requests.get('https://in.bookmyshow.com/buytickets/dangal-hyderabad/movie-hyd-ET00033292-MT/20161225')
this Object contains the HTML DOM in string format which can be converted to HTML DOM object as follows
tree = html.fromstring(page.content)
If we clearly analyse the HTML, we get to know that the Theatre names are embedded in strong tags. These can be gathered using xpath function on the tree object obtained in the previous step as follows
theatres = tree.xpath('//strong/text()')
Now the theatres is a list of all string objects which contains all the theatre’s names which can be iterated as required theatre can be mapped using simple string manipulations steps.
Step 5 a:
If required theatre is found then get notified by either playing any song in the laptop or by getting an email etc.
Songs can be played using the os.startfile function which contains parameters as the path of song in the system
Step 5 b:
If required theatre is not found that means the bookings have not started in that theatre still. So trigger the page again after a particular time which can be done by triggering the sleep function on the time library.