Hi to all,
While doing my project , i have to get current temperature for over all india cities.
For World : Current Temperature
http://www.timeanddate.com/weather/
For India : Current Temperature
http://www.timeanddate.com/weather/india
I wrote one webscrap program to get all india cities current temperature from the above link.
And after running this python program then it will create “cities_and_its_temperatures_in_degree.txt” in your working directory.
To run this program , we need to install BeautifulSoup package.
$ sudo apt-get install python-setuptools
$ sudo easy_install BeautifulSoup
or install from the BeautifulSoup Source Package.
Webscrap_current_temperature.py
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
PasteBin :
Please use pastebin site to download the above program.
Output on 23/03/2010 at 08.01 am. Its getting current temperature for around 404 cities in India.
I hope this may help to some one.
By
Arulalan.T
Posted by Bert on December 5, 2011 at 11:59 pm
Thanks for posting! This is a really interesting script. I tried running it, but I did not get the data in the text file. I copied what was written to the text file below.
current temperature in all cities of india at this moment <a href="/custom/site.html" onclick="return jco(this, '/worldclock/wccust.php?cb=66442&type=w
Is this because I am running python in windows?