【问题标题】:Unable to parse data from weather website无法解析来自天气网站的数据
【发布时间】:2017-01-12 00:21:41
【问题描述】:

嘿,我想从以下网站解析天气数据:http://www.indiaweather.gov.in

如果您发现您有一个搜索栏,可让您输入任何印度城市的名称,当您点击“Go”时,您最终会进入包含该城市天气数据的页面。

谁能帮我写一个脚本,让我输入城市名称,并显示城市的天气数据?

我不知道如何解析数据。

谢谢!

【问题讨论】:

  • 在 gitter 上找到我会告诉你如何做到这一点我的昵称:dexax
  • @Vin,如果你不知道,那么你应该聘请开发人员为你做这件事。

标签: python html parsing beautifulsoup weather


【解决方案1】:

你可以用美丽的汤!在特定的 html 标记之间获取所需的数据。如果你想自己做,你可以通过调用网站的 api 来发布你的数据。首次安装请求。 pip install requests然后

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'])

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 2013-11-27
  • 2020-08-06
相关资源
最近更新 更多