-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrape.py
executable file
·65 lines (58 loc) · 1.9 KB
/
scrape.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
#!/usr/bin/env python3
from util import *
import pandas as pd
OUTFILE = "cafes.geojson"
df = pd.read_csv("locations.csv")
# Ignore locations that have already been scraped
locations = df.name[df.n_places.isna()]
print(f"Have {len(locations)} locations")
locations = iter(tqdm(locations))
driver = initialise_driver()
location = next(locations)
location_type = 'cafes'
search = f"{location_type} in {location}"
print(search)
url = f"https://www.google.com/maps/search/{search}?hl=en"
print(url)
driver.get(url)
features = {}
load(features, OUTFILE)
while True:
try:
n_places = extract_page(driver, features)
print(f"Got {n_places} places for {location}")
save(features, OUTFILE)
# Record that we've scraped this location
df.scraped_at[df.name == location] = pd.Timestamp.now()
df.n_places[df.name == location] = n_places
df.to_csv("locations.csv", index=False)
try:
location = next(locations)
except StopIteration:
print("All locations complete")
break
search = f"{location_type} in {location}"
print(search)
driver.get(f"https://www.google.com/maps/search/{search}?hl=en")
except KeyboardInterrupt:
print("Interrupted by user, will now save")
break
except Exception as e:
# Some other Exception - try reload the whole page and start over
print(f"ERROR: {e}")
import traceback
traceback.print_exc()
try:
with open("error.html", "w") as f:
f.write(driver.page_source)
driver.save_screenshot("error.png")
except:
print("can't even write error.html...")
driver = initialise_driver()
# Restart
driver.get(f"https://www.google.com/maps/search/{search}?hl=en")
save(features, OUTFILE)
try:
driver.close()
except:
print("Unable to close webdriver")