-
Notifications
You must be signed in to change notification settings - Fork 2
/
unica_parser.py
81 lines (76 loc) · 2.69 KB
/
unica_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
import urllib2
import re
import sys
import csv
from slimmer import html_slimmer
from HTMLParser import HTMLParser
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
def urlread(url):
f = urllib2.urlopen(url)
p = f.read()
return p
restaurants = ["tottisalmi", "arcanum", 'assarin-ullakko', 'brygge', 'delica', 'deli-pharma', 'dental', 'macciavelli', 'mikro', 'myssy-silinteri', 'nutritio', 'ruokakello']
restaurants_lunchlists = {}
for restaurant in restaurants:
html = urlread("http://www.unica.fi/fi/ravintolat/" + restaurant +"/")
html = unicode(html, 'utf-8')
parsed_html = BeautifulSoup(html)
try:
lunchlist = {}
lunchlist_html = parsed_html.body.find('div', attrs={'class':'menu-list'})
for day_div in lunchlist_html.find_all('div', attrs={'class':'accord'}):
if(restaurant == "arcanum"): print day_div
day = day_div.find('h4').text
tmp = []
try:
for lunch in day_div.find_all('tr'):
lunch_name = lunch.find('td', attrs={'class':'lunch'})
lunch_limitations = lunch.find('td', attrs={'class':'limitations quiet'})
lunch_price = lunch.find('td', attrs={'class':'price quiet'})
lunch_limitations = (re.sub('\n', ' ', re.sub('\t', '', lunch_limitations.text))).strip()
lunch_price = re.sub('Hinta:', '',(re.sub('\n', ' ', re.sub('\t', '', lunch_price.text)))).strip()
tmp.append([lunch_name.text.encode('utf-8'), lunch_limitations.encode('utf-8'), lunch_price.encode('utf-8')])
lunchlist[day] = tmp
except:
lunchlist[day] = [["Ei lounasta", " ", " "]]
pass
restaurants_lunchlists[restaurant] = lunchlist
except:
pass
for restaurant in restaurants_lunchlists:
lunchlist = restaurants_lunchlists[restaurant]
with open('lunchlist/' + restaurant + '.csv', 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
writer.writerow(['DAY', 'LUNCH', 'LIMITATIONS', 'PRICE'])
for day in lunchlist.keys():
for lunch in lunchlist[day]:
writer.writerow([day] + [lunch[0]] + [lunch[1]] + [lunch[2]])
"""
print "Tällä viikolla herkkuruokaa tarjoaa: " + restaurant.capitalize()
print "\n"
print "Katsotaanpa mitä hyvää on tarjolla.."
print "\n"
for day in lunchlist.keys():
print day
print "\n"
lunches = lunchlist[day]
for lunch in lunches:
print lunch[0] + ' - ' + lunch[1]
print "Hinta " + lunch[2]
print "\n"
print "\n"
print "-------\n"
"""