
Arulalan.T/ WebScraping Program to scrap current temperature for over all india - cities
By: a guest on Mar 22nd, 2010 | syntax:
Python | size: 3.03 KB | hits: 253 | expires: Never
from BeautifulSoup import BeautifulSoup
import re
import urllib
import unicodedata
import os
#open the url
print("Start Reading the website- this may take some time - approx 2 mins ")
filecontent= urllib.urlopen('http://www.timeanddate.com/weather/india').read()
#convert it into beautiful soup content
soupcontent = BeautifulSoup(filecontent)
print("Printing the values, which have been parsed")
#write the whole parsed soupcontent into temp.txt file
f=open('temp.txt','w')
s=str(soupcontent)
f.write(s)
f.close()
#find and write the long string which contaning all the cities temperature values inside table from temp.txt into content.txt
file1=open('temp.txt','r')
for line in file1:
loc=line.find("agra")
if loc is not -1:
content=line
file1.close()
file2=open('content.txt','w')
file2.write(content)
file2.close()
file3=open('content.txt','r')
file4=open('cities_and_its_temperatures_in_degree.txt','w')
# find the temperature degree value from this line array by reverse finding method and writes into deg,city variable. then reverse those string variable value and write into cities_and_its_temperatures_in_degree.txt file
for line in file3:
#To find and Current time from the web link
time_col=line.find('img')
date_and_time=''
time_col=time_col-21
date_and_time=date_and_time+line[time_col]
flag0=1
while flag0:
time_col=time_col-1
date_and_time=date_and_time+line[time_col]
if line[time_col-1]==">":
flag0=0
date_and_time = date_and_time[::-1]
first_line="current temperature in all cities of india at this moment "+date_and_time
file4.write(first_line)
file4.write('\n')
#To find degree of temp
found=line.find(' °C')
while found > -1:
flag=1
column=found
deg=''
while flag:
column=column-1
if line[column-1]==">":
flag=0
deg=deg+line[column]
deg = deg[::-1]# reverse the string value
city=''
flag1=1
while flag1:
column=column-1
if line[column-1]==">":
if line[column-2]=="a":
column=column-5
while flag1:
city=city+line[column]
column=column-1
if line[column]==">":
flag1=0
city = city[::-1]# reverse the string value
city_and_temp=''
city_and_temp=city+' '+deg+'\n'
file4.write(city_and_temp)
#print city
#print deg
found=line.find(' °C', found+1)
file3.close()
file4.close()
print "cities_and_its_temperatures_in_degree.txt has successfully created\n please open this file to view all cities current temperature in degree"
os.remove('temp.txt')
os.remove('content.txt')
print date_and_time