forked from jjjiia/menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initial_crawl.py
executable file
·158 lines (139 loc) · 5.12 KB
/
initial_crawl.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
import urllib2
import sys
import json
from bs4 import BeautifulSoup, NavigableString
import re
import csv
import datetime
import os
duplicates = []
latestDictionary = {}
import time
basecity = "boston"
baseURL1 = ".menupages.com/restaurants/"
baseURL2 = "/all-neighborhoods/all-cuisines/"
neighborhoods = ["cambridge"]
#neighborhoods = ["downtown-north-end","back-bay-beacon-hill-south-end","fenway-symphony-jamaica-plain","brookline-brighton-allston"]
def download_craigslist(page_count = 1, limit = 5):
data = []
duplicateCount = 0
uniqueCount = 0
page_count = 48
for i in range(page_count):
#for neightborhoods
#link = 'http://'+basecity+baseURL1+baseURL2 + str(i+1)
#for larger areas
link = 'http://'+basecity+baseURL1 +city+baseURL2 + str(i+1)
print link
soup = BeautifulSoup(urllib2.urlopen(link).read())
#x = soup.body.article.section.div
x = soup.find("div",{"id":"searchresults"})
#print x
title = []
time = []
art = []
for tag in x.find_all('a',{"class":"link"}):
linkOnly = str(tag).split("\"")[3]
#print tag.split("\"")[1]
k=0
art.append(linkOnly)
#link = tag.find_all('a')
#print link
# art.append(ah['href'])
#print art
for l in art:
data = []
url = "http://"+basecity+".menupages.com"+l+"menu"
#print url
sidesoup = BeautifulSoup(urllib2.urlopen(url).read())
menu = sidesoup.find("div",{"id":"restaurant-menu"})
table_body = menu.find_all('tbody')
rows = menu.find_all('tr')
latlng = sidesoup.find("a",{"title":"open Google Maps in a new window"})
#print address
latlngIndex = str(latlng).find("markers")
#print addressIndex
coordinates = str(latlng)[latlngIndex:latlngIndex+30]
coordinates = coordinates.split("=")[1]
coordinates = coordinates.split("&")[0]
#print coordinates
zipcode = sidesoup.find("span",{"class":"addr street-address"})
zipcode = re.sub("<.*?>", " ", str(zipcode).lower())
address = sidesoup.find("span",{"class":"postal-code"})
address = re.sub("<.*?>", " ", str(address).lower())
restType = sidesoup.find("title")
restType = re.sub("<.*?>", " ", str(restType).lower())
#print restType
restType = str(restType).split("menupages")[1]
restType = restType.split("search")[0]
print restType
#print zipcode, address
for row in rows:
#print row
test = re.sub("<.*?>", " ", str(row).lower())
test = re.sub("-", "", test)
test = re.sub(r"[\xc2\xa0\xe2\x80\x99]","",test)
test = re.sub('\d\d\W\d\d','',test)
test = re.sub('\d\W\d\d','',test)
test = re.sub('\W\d\d','',test)
test = test.strip()
#prices = re.search('\d\W\d\d', test)
#print prices
##prices = re.sub(".", " ", prices)
test = re.sub("&","", test)
test = re.sub(","," ", test)
test = re.sub(":","", test)
test = re.sub("-","", test)
test = re.sub("\*","", test)
test = re.sub("\.","", test)
test =" ".join(test.split())
#print test
restaurantName = str(l).split("/")[2]
#data.append(test+" "+restaurantName)
#cleanMenu = [str(l),data]
#print test+" "+restaurantName
spamwriter.writerow([test,restaurantName,coordinates,address,zipcode,[restType]])
#print [test,restaurantName,coordinates]
print restaurantName
#cities = ["brookline-brighton-allston","downtown-north-end", "fenway-symphony-jamaica-plain","back-bay-beacon-hill-south-end"]
#neighborhoods = ["cambridgeport","harvard-square","east-cambridge","central-sq-inman-sq","porter-sq-fresh-pond"]
if __name__ == "__main__":
if len(sys.argv) >= 2 and sys.argv[1] == "crawl":
if len(sys.argv) <= 2:
page_count = 1
else:
page_count = sys.argv[2]
if len(sys.argv) <= 3:
limit = 5
else:
limit = sys.argv[3]
print "Crawling Craigslist"
print "Page Count: " + str(page_count)
print "Limit: " + str(limit)
#print json.dumps(download_craigslist(int(page_count), int(limit)))
# json.dump(json.dumps(download_craigslist(int(page_count), int(limit))), outfile)
for i in range(len(neighborhoods)):
time.sleep(10)
city = neighborhoods[i]
print city
outputfile = open(city+"_menus_address.csv","wb")
spamwriter = csv.writer(outputfile)
#spamwriter.writerow(["url","menu"])
while True:
try:
download_craigslist(int(page_count), int(limit))
sys.exit(0)
except Exception, e:
i+=1
print e
break
elif len(sys.argv) >= 2 and sys.argv[1] == "serve":
print "Server not implemented yet."
else:
print """
Usage crawler:
python craigslist.py crawl <page_count> <limit>
Usage server:
python craigslist.py serve
"""