import urllib from BeautifulSoup import BeautifulSoup from time import sleep # Scan gyms which give vouchers # Important: Run this script outside IDLE (IDLE/BS bug) for postnbr in range(10, 20): postnbr_s = '%04d0' % postnbr params = urllib.urlencode({'Code': postnbr_s}) page = urllib.urlopen("http://www.leadfilter.fi/gym/r.php", params) content = page.read() soup = BeautifulSoup(content) if 'Valitettavasti alueellasi' in content: print postnbr_s, 'Ei ole' else: print postnbr_s, 'On' places = soup.findAll(attrs={'class': '.no_voucher'}) for place in places: print '-', place.contents[0] sleep(1) # to prevent flooding the site # Sample run: # # F:\omat\py\temp>python temp.py # 00100 On # - Idealfit - Eira # - Move! Wellness Center # - Tanssikoulu Salsa de Cuba # - Kuntokeskus ProGym # 00110 Ei ole # 00120 Ei ole # 00130 On # - Idealfit - Eira # 00140 On # - Idealfit - Eira # 00150 On # - Idealfit - Eira # 00160 On # - Idealfit - Eira # 00170 On # - Idealfit - Eira # 00180 On # - Idealfit - Eira # 00190 On # - Idealfit - Eira