-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathrun.py
36 lines (27 loc) · 1.35 KB
/
run.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
"""
This example run script shows how to run the tripadvisor scraper defined in ./tripadvisor.py
It scrapes hotel data and saves it to ./results/
To run this script set the env variable $SCRAPFLY_KEY with your scrapfly API key:
$ export $SCRAPFLY_KEY="your key from https://scrapfly.io/dashboard"
"""
import asyncio
import json
from pathlib import Path
import tripadvisor
output = Path(__file__).parent / "results"
output.mkdir(exist_ok=True)
async def run():
# enable scrapfly cache for basic use
tripadvisor.BASE_CONFIG["cache"] = False
print("running Tripadvisor scrape and saving results to ./results directory")
result_location = await tripadvisor.scrape_location_data(query="Malta")
output.joinpath("location.json").write_text(json.dumps(result_location, indent=2, ensure_ascii=False))
result_search = await tripadvisor.scrape_search(query="Malta", max_pages=2)
output.joinpath("search.json").write_text(json.dumps(result_search, indent=2, ensure_ascii=False))
result_hotel = await tripadvisor.scrape_hotel(
"https://www.tripadvisor.com/Hotel_Review-g190327-d264936-Reviews-1926_Hotel_Spa-Sliema_Island_of_Malta.html",
max_review_pages=3,
)
output.joinpath("hotels.json").write_text(json.dumps(result_hotel, indent=2, ensure_ascii=False), encoding="utf-8")
if __name__ == "__main__":
asyncio.run(run())