-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_obj.py
47 lines (38 loc) · 1.21 KB
/
update_obj.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
# encoding=utf8
from lxml import html
import BeautifulSoup
from bs4 import BeautifulSoup
import pickle
import urllib2
import time
import threading
import anywhereselenium
urlFi = 'http://www.quadrifoglio.org/scrivi_orario_strade_tutte.php?comune=FIRENZE'
urlSf = 'http://www.quadrifoglio.org/scrivi_orario_strade_tutte.php?comune=SESTO%20FIORENTINO'
start = time.time()
urls = [urlFi, urlSf]
listaFi = []
listaSf = []
def save_obj(obj, name):
with open('obj/' + name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def fetch_url(url):
urlHandler = urllib2.urlopen(url)
html = urlHandler.read().decode('UTF-8')
print "'%s\' fetched in %ss\nParsing html..." % (url, (time.time() - start))
soup = BeautifulSoup(html, "lxml")
for tag in soup.find_all('div'):
if url == urlFi:
listaFi.append(tag.text)
save_obj(listaFi, 'listaFi')
else:
listaSf.append(tag.text)
save_obj(listaSf, 'listaSf')
threads = [threading.Thread(target=fetch_url, args=(url,)) for url in urls]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
print "Elapsed Time: %s" % (time.time() - start)
print "Pickles saved in /obj."
anywhereselenium.automate()