Monday, 14 November 2016

web scrapping with python BeautifulSoup


Web scrapping series :

(Dig out web data)

Part 1:

URL used for scrapping :   http://www.assamyellowpage.com

Today i am going to dig out data from a website without visiting the website . Say we are going to search a word (jalukbari) in the site http://www.assamyellowpage.com . It will look like bellow .


 

In our result we are getting like some result .

Now my script should dig out same data from the site ,


import requests
from bs4 import BeautifulSoup
import os
a=raw_input('Enter your query:')
r=requests.get('http://www.assamyellowpage.com/search?title=&street=%s&taxonomy_vocabulary_10_tid=All'%a)
soup=BeautifulSoup(r.content)
g_data=soup.find_all("div",{"class":"item-list"})
os.system('clear')
for i in range(len(g_data[0].find_all("div",{"class":"views-field views-field-title"}))):
        print 'Institiute-'+str(i),":"+g_data[0].find_all("div",{"class":"views-field views-field-title"})[i].text
        print '\nDescription:',g_data[0].find_all("span",{"class":"field-content"})[i].text


After running the script you will get the following result :





 







We got the exact same result as we got from above search  .Bellow is another search result .










Enjoy . We have just created a API for this site  http://www.assamyellowpage.com.

In next article i will scrap data from different sites like facebook.com, google.com , twitter.com and and many more .

Connect With Me: Facebook


Tuesday, 8 November 2016

export Data from Mysql server to excel file using python

Export Data from Mysql server to excel file using python:

I have data on mysql server as following



Now i want these data to export to  a excel file like bellow .





 Just follow as bellow .

 Main Python Script:


import mysql.connector
from xlwt import Workbook
con=mysql.connector.connect(host='xxx',database='xxx',user='xxx',password='xxxx')
cur=con.cursor()
wb=Workbook()
sheet1=wb.add_sheet('sheet1')
sql="""select * from employee """
cur.execute(sql)
result=cur.fetchall()
sheet1.write(0,0,"First Name")
sheet1.write(0,1,"Last Name")
sheet1.write(0,2,"Age")
sheet1.write(0,3,"Gender")
sheet1.write(0,4,"Income")
row_number=1
for row in result:
        column_number=0
        for item in  row:
                sheet1.write(row_number,column_number,str(item))
                column_number=column_number+1
        row_number=row_number+1
wb.save('test1.xls')



Thats it . Just fill the following part by yourself .

host='xxx',database='xxx',user='xxx',password='xxxx'


Thursday, 8 September 2016

Bus time prediction script in Chicago , USA with python

Bus time prediction script in USA with python :  This script will tell you at what time the nest Bus coming towards your way in Chicago ,USA .

 Here i am using following website for route and Bus prediction .

http://ctabustracker.com/bustime/home.jsp

Lets  create a file called 1.py and copy the following code to it .

import urllib.request
import sys
if len(sys.argv) !=3:
        raise SystemExit('We need route and stopid.')
route=sys.argv[1]
stopid=sys.argv[2]
u=urllib.request.urlopen('http://ctabustracker.com/bustime/map/getStopPredictions.jsp?stop={}&route={}'.format(stopid,route))
data=u.read()
from xml.etree.ElementTree import XML
doc=XML(data)
for pt in doc.findall('.//pt'):
        print(pt.text)

Now  save the file and run this as :  python3.4 1.py 22 14787

here 22 is route  and 14787 is bus-stop .

You will get following result :


 Enjoy . :)

Connect With Me: Facebook 

Monday, 5 September 2016

Python script to find a string (word) palindrome or not

Today this python script that will show whether a string is palindrome  or not .

This script will work on pythoon3 and above .

copy bellow script to a file say its 1.py

a=str(input('Give a string: '))
d=len(a)
b=[]
for i in range (d):
        b.append(a[i])
c=[]
e=len(b)
for i in range(e):
        c.append(b[(e-1)-i])
if b==c:
        print('Given',a,'is a palindome.')
else:
        print('No',a,'is not a palindrame.')


 Now Run the script as :   python3.4 1.py


You will get the following output .




Popular Posts