-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtripadvisor_restaurants.py
More file actions
49 lines (41 loc) · 1.61 KB
/
tripadvisor_restaurants.py
File metadata and controls
49 lines (41 loc) · 1.61 KB
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
import argparse
import csv
import requests
from selectorlib import Extractor
from formatter_classes import formatters
def write_to_file(response):
# writes HTML response to a file for debugging purpose
with open("response.html", "w") as fp:
fp.write(response.text)
def process_page(url):
# Create an Extractor by reading from the YAML file
e = Extractor.from_yaml_file(
'tripadvisor_restaurants.yml',
formatters=formatters)
# Download the page using requests
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'} # noqa
print('Downloading Page')
r = requests.get(url, headers=headers, timeout=30)
# Pass the HTML of the page and create
print("Parsing Page")
data = e.extract(r.text)
if not data['restaurants']:
write_to_file(r)
return data
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument('url', help='Tripadvisor Listing URL')
args = argparser.parse_args()
scraped_data = process_page(args.url)
if scraped_data:
print("Writing scraped data to restaurants.csv")
with open('restaurants.csv', 'w') as csvfile:
fieldnames = ["name", "reviews", "cusine",
"rating", "price_range", "url"]
writer = csv.DictWriter(
csvfile, fieldnames=fieldnames, quoting=csv.QUOTE_ALL)
writer.writeheader()
for data in scraped_data['restaurants']:
writer.writerow(data)
else:
print("No data scraped")