Showing posts with label beautiful soup. Show all posts
Showing posts with label beautiful soup. Show all posts

Thursday, 1 June 2017

PNR Status Using Python Script

Hello friends,

In this post, we'll see a Python script to fetch the PNR status of a railway ticket for a given PNR information. The script use Rail Yatri to get the PNR status. The library beautiful soup is used to extract the relevant information. The script prompts user for a PNR number and then fetches information from Rail Yatri for the PNR.

#! /usr/bin/python
import requests
from bs4 import BeautifulSoup
from time import sleep

try:
 pnr = input('Enter PNR : ')
 url = 'http://www.railyatri.in/pnr-status/' + pnr
 html = requests.get(url)
 soup = BeautifulSoup(html.text, 'lxml')
 crntStatus = soup.findAll('span', {'class':'arrival_span'})
 bkngStatus = soup.findAll('span', {'class':'departure'})
 print(bkngStatus)
 try:
  print('PNR Status', 'Booking Satus: ' + bs[1].text + '\n' + 'Curent Satus: ' + cs[2].text) 
 except:
  print("Invalid PNR. Try again!")

except requests.exceptions.ConnectionError:
 print("Connection Error. Try again!")

Share:

Wednesday, 31 May 2017

Movie/TV Series Rating Using IMDB

Hi friends,

In this post, we'll see a python script that takes a movie or a TV series name as an input and fetches its IMDB rating and summary from the IMDB website. The script uses Beautiful soup library in Python to extract the ratings and summary from the website. Here is the script to achieve the task.

import requests
from bs4 import BeautifulSoup

def getTitle(url):
    html = requests.get(url)
    soup = BeautifulSoup(html.text,'lxml')
    for title in soup.findAll('div',{'class':'title_wrapper'}):
        return title.find('h1').text.rstrip()

def getInfo(movieUrl):
    html = requests.get(movieUrl)
    soup = BeautifulSoup(html.text,'lxml')
    for div in soup.findAll('div',{'class':'ratingValue'}):
        print ('IMDB rating of \'' + userInput + '\' is: ', div.text)
        print ()

    for div in soup.findAll('div',{'class':'summary_text'}):
        print ('Summary of  \'' + userInput + '\' : ')
        print (div.text.lstrip())

userInput = input(('Enter Movie/Tv series name : '))
print ()
url = 'http://www.imdb.com/find?ref_=nv_sr_fn&q=' + userInput + '&s=all'

html = requests.get(url)
soup = BeautifulSoup(html.text,'lxml')

for td in soup.findAll('td',{'class':'result_text'}):
    link = td.find('a')['href']
    movieUrl = 'http://www.imdb.com' + link
    break

name = getTitle(movieUrl)
getInfo(movieUrl)
Share:

Live Cricket Scores Using Python Script

Hi friends,

In this post, we'll see a python script to get the live cricket score of a particular match using espncricinfo site. The script browses the espncricinfo website and lists all the live matches going on. The user can then select a specific match from the list and the script shows the live scores of the selected match. The script also fetches the score of the selected match after every 20 seconds which can be changed accordingly. So, here is the script to see live scores of a cricket match. 

import requests
from bs4 import BeautifulSoup
from time import sleep
import sys

print('Live Cricket Matches:')
print('=====================')
url = "http://static.cricinfo.com/rss/livescores.xml"
r = requests.get(url)
soup = BeautifulSoup(r.text,'lxml')

i = 1
for item in soup.findAll('item'):
 print(str(i) + '. ' + item.find('description').text)
 i = i + 1

links = []    
for link in soup.findAll('item'):
 links.append(link.find('guid').text)

print('Enter match number or enter 0 to exit:')
while True:
 try:
  userInput = int(input())
 except NameError:
  print('Invalid input. Try Again!')
  continue
 except SyntaxError:
  print('Invalid input. Try Again!')
 if userInput < 0 or userInput > 30:
  print('Invalid input. Try Again!')
  continue
 elif userInput == 0:
  sys.exit()      
 else:
  break

url = links[userInput - 1]
r = requests.get(url)
soup = BeautifulSoup(r.text,'lxml')  

while True:
 matchUrl = links[userInput - 1]
 r = requests.get(matchUrl)
 soup = BeautifulSoup(r.text,'lxml') 
 score = soup.findAll('title')       
 try:
  r.raise_for_status()
 except Exception as exc:
  print ('Connection Failure. Try again!')
  continue
 print(score[0].text + '\n')
 sleep(20)

Share:

Contact Me

Name

Email *

Message *

Popular Posts