Advertisement
Guest User

Arulalan.T/ WebScraping Program to scrap current temperature for over all india - cities

a guest
Mar 22nd, 2010
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. from BeautifulSoup import BeautifulSoup
  2. import re
  3. import urllib
  4. import unicodedata
  5. import os
  6.  
  7. #open the url
  8. print("Start Reading the website- this may take some time - approx 2 mins ")
  9. filecontent= urllib.urlopen('http://www.timeanddate.com/weather/india').read()
  10. #convert it into beautiful soup content
  11. soupcontent = BeautifulSoup(filecontent)
  12.  
  13. print("Printing the values, which have been parsed")
  14.  
  15. #write the whole parsed soupcontent into temp.txt file
  16. f=open('temp.txt','w')
  17. s=str(soupcontent)
  18. f.write(s)
  19. f.close()
  20.  
  21. #find and write the long string which contaning all the cities temperature values inside table from temp.txt into content.txt
  22. file1=open('temp.txt','r')
  23. for line in file1:
  24.     loc=line.find("agra")
  25.     if loc is not -1:
  26.         content=line
  27. file1.close()
  28.  
  29.  
  30. file2=open('content.txt','w')
  31. file2.write(content)
  32. file2.close()
  33.  
  34.  
  35. file3=open('content.txt','r')
  36. file4=open('cities_and_its_temperatures_in_degree.txt','w')
  37.  
  38.  
  39. # 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
  40. for line in file3:
  41.    
  42.  
  43.     #To find and Current time from the web link
  44.     time_col=line.find('img')
  45.     date_and_time=''
  46.     time_col=time_col-21
  47.     date_and_time=date_and_time+line[time_col]
  48.     flag0=1
  49.     while flag0:
  50.         time_col=time_col-1
  51.         date_and_time=date_and_time+line[time_col]
  52.         if line[time_col-1]==">":
  53.             flag0=0
  54.     date_and_time = date_and_time[::-1]
  55.     first_line="current temperature in all cities of india at this moment "+date_and_time
  56.     file4.write(first_line)
  57.     file4.write('\n')
  58.     #To find degree of temp
  59.     found=line.find(' °C')
  60.     while found > -1:
  61.         flag=1
  62.         column=found
  63.         deg=''
  64.         while flag:        
  65.             column=column-1            
  66.             if line[column-1]==">":
  67.                 flag=0
  68.  
  69.             deg=deg+line[column]
  70.         deg = deg[::-1]# reverse the string value
  71.        
  72.         city=''
  73.  
  74.         flag1=1
  75.         while flag1:        
  76.             column=column-1            
  77.             if line[column-1]==">":
  78.                 if line[column-2]=="a":
  79.                     column=column-5
  80.                     while flag1:
  81.                         city=city+line[column]
  82.                         column=column-1
  83.                         if line[column]==">":
  84.                            flag1=0
  85.                        
  86.         city = city[::-1]# reverse the string value
  87.  
  88.         city_and_temp=''
  89.         city_and_temp=city+'        '+deg+'\n'          
  90.         file4.write(city_and_temp)
  91.        
  92.         #print city
  93.         #print deg
  94.  
  95.         found=line.find(' °C', found+1)
  96.  
  97. file3.close()
  98. file4.close()
  99. print "cities_and_its_temperatures_in_degree.txt has successfully created\n please open this file to view all cities current temperature in degree"
  100.  
  101. os.remove('temp.txt')
  102. os.remove('content.txt')
  103. print date_and_time
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement