Syildiz
Syildiz

Reputation: 79

BeautifulSoup Table Scraping

I am new at Python and BeautifulSoup. I actually handled weather data grabing from wunderground.com however I have to grab some datas from freemeteo.com too. Therefore I have severeal question.

FIRST ISSUE

For historical data I used at wunderground.com below code (Note: wunderground.com has this kind of links www.wunderground.com/history/airport/LTBA/2013/1/1/)

for y in range(2009, 2014):
    for m in range(1, 13):
    for d in range(1,32):
        if y%400 == 0:
            leap = True
        elif y%100 == 0:
            leap = False
        elif y%4 == 0:
            leap = True
        else:
            leap = False
        if (m == 2 and leap and d > 29):
            continue
        elif (m == 2 and d > 28):
            continue
        elif (m in [4, 6, 9, 10] and d > 30):
            continue

        url ="http://www.wunderground.com/history/airport/LTBJ/" + str(y) + "/" + str(m) + "/" + str(d) + "/DailyHistory.html"

Str() was ok for wunderground.com links however freemeteo.com has below kind of links.

tr.freemeteo.com/havadurumu/antioch/history/daily-history/?gid=323779&station=8768&date=2009-01-01&language=turkish&country=turkey

As you can see, there is "0" in front of the date text for 1 to 10 numbers. That is mean I have to add some code for those links.

SECOND ISSUE

freemeteo.com has tables in the pages as linked pic.

As you can see in the pic I just want to get red box column data from that table and get .txt file as an below format. I need help for this. It is going to use in my thesis. Thank you for your all attentions.

20090101,00:00,1°C,-2°C,11 Km/h,75%,-3°C
20090101,01:00,0°C,-3°C,7 Km/h,75%,-4°C
20090101,02:00,-1°C,-4°C,7 Km/h,80%,-4°C,

Upvotes: 0

Views: 280

Answers (1)

mattexx
mattexx

Reputation: 6606

Use % to format the numbers with a leading zero:

url ="http://www.wunderground.com/history/airport/LTBJ/" + str(y) + "/" + "%02d" % m + "/" + "%02d" % d + "/DailyHistory.html"

https://docs.python.org/2/library/stdtypes.html#string-formatting

Upvotes: 0

Related Questions