-2

Hey i want to parse weather data from the following website: http://www.indiaweather.gov.in

If you observe you have a search bar which lets you enter any indian city's name and when you hit go you end up on a page with the weather data for that city.

Could anyone help me with a script which will allow me to enter the city's name, and will display the city's weather data?

I didn't know how to parse the data.

Thanks!

Vin
  • 729
  • 9
  • 15

1 Answers1

-1

You could use Beautiful Soup! to get your desired data between specific html tags. If you want to do it yourself you could post your data by calling the site's api. first install requests. pip install requests then

import re
import requests


# the data is between align="left"><font size="3px"> and </font> in each specific part so we use re to find it
def find_data(line):
    return re.search('align="left"><font size="3px">(.*?)</font>', line).group(1)

params = {
    'tags': city_name,
    'submit': 'Go'
}

response = requests.post('http://www.indiaweather.gov.in/?page_id=942', data=params).content.splitlines()

try:
    current_temp = find_data(response[357])
    max_temp = find_data(response[362])
    min_temp = find_data(response[369])
    last_day_rainfall = find_data(response[376])
    current_wind_direction = find_data(response[382])
    current_wind_speed = find_data(response[389])
    current_dew_point = find_data(response[396])
    current_sea_level_pressure = find_data(response[403])
    print(current_temp, max_temp, min_temp, last_day_rainfall, current_wind_direction, current_wind_speed,
          current_dew_point, current_sea_level_pressure)

except IndexError:
    print('No Info Found For %s' % params['tags'])
Amin Etesamian
  • 3,363
  • 5
  • 27
  • 50