Review Python script for getting the stats of codereview.SE into a file for analysis
Firstly, you don't need to store the last date at the beginning of the file since it's already at the end.

Secondly, you shouldn't compute today's date twice.

Thirdly, you should use Stack Exchange's API instead of trying to extract information from Code Review's home page. It has everything except visitors/day.

Fourthly, the date format %Y-%m-%d is better than %d-%m-%Y because it allows string comparison of dates.

So, here's what I came up with:

#!/usr/bin/env python3

import datetime
import gzip
import json
from urllib.request import Request, urlopen

FILE_NAME = 'data_file.txt'
API_URL = 'http://api.stackexchange.com/2.1/info?site=codereview'

def last(iterable, default):
    r = default
    for v in iterable:
        r = v
    return r

def main():
    today = datetime.date.today().strftime('%Y-%m-%d')
    with open(FILE_NAME, 'a+') as f:
        f.seek(0)
        last_date = last(f, '').split(',')[0]
        if last_date == today:
            return  # we already have the data for today
        elif last_date &gt; today:
            raise Exception('last date is in the future')

        req = Request(API_URL, headers={'Accept-Encoding':'gzip'})
        r = gzip.decompress(urlopen(req).read()).decode('utf8')
        d = json.loads(r)['items'][0]
        print(today,
            d['total_questions'],
            d['total_answers'],
            d['total_unanswered'],
            d['total_users'],
            sep=',', file=f)

if __name__ == "__main__":
    main()


The gzip stuff is to work around a bug in Stack Exchange's API server: it always compresses the response even if you don't ask, which is a protocol violation. Using the requests library might make it easier.

The way I get the last line of the file isn't the most efficient but it should be good enough.