【问题标题】:Parsing a Table from the following website从以下网站解析表格
【发布时间】:2018-04-26 23:56:11
【问题描述】:

我想收集印度某个城市在 2016 年每一天的过去天气详细信息。以下网站有这些数据:

https://www.timeanddate.com/weather/india/kanpur/historic?month=1&year=2016

此链接包含 2016 年 1 月的数据。那里有一张不错的表格

I want to extract this table

I have tried enough and I could extract another table which is this one. BUT I DO NOT WANT THIS ONE. It is not serving my purpose

我想要另一个大表,其中包含随时间给出的数据。 “对于该月的每一天”,因为这样我就可以使用 URL 遍历所有月份。

问题是我不知道 html 和与之相关的东西。所以我不能自己刮东西。

【问题讨论】:

    标签: python-2.7 beautifulsoup html-parsing


    【解决方案1】:

    如果您提供一些您尝试过的代码会更好。无论如何,此代码适用于 1 月 1 日的表。您也可以编写循环来提取其他日子的数据。

    from urllib.request import urlopen
    from bs4 import BeautifulSoup
    url = "https://www.timeanddate.com/weather/india/kanpur/historic?
    month=1&year=2016"
    page = urlopen(url)
    soup = BeautifulSoup(page, 'lxml')
    
    Data = []
    table = soup.find('table', attrs={'id':'wt-his'})
    for tr in table.find('tbody').find_all('tr'):
       dict = {}
       dict['time'] = tr.find('th').text.strip()
       all_td = tr.find_all('td')
       dict['temp'] = all_td[1].text
       dict['weather'] = all_td[2].text
       dict['wind'] = all_td[3].text
       arrow = all_td[4].text
       if arrow == '↑':
          dict['wind_dir'] = 'South to North'
       else: 
          dict['wind_dir'] = 'North to South'
    
       dict['humidity'] = all_td[5].text
       dict['barometer'] = all_td[6].text
       dict['visibility'] = all_td[7].text
    
       Data.append(dict)
    

    注意:为 wind_dir 逻辑添加其他情况

    【讨论】:

      猜你喜欢
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多