Untitled

 avatar
user_3839718
python
a month ago
1.6 kB
2
Indexable
Never
import json
from outscraper import ApiClient
from config import outscraper_key


def crawl_review(place_data_path, saved_reviewed_path, reviews_limit=100, language='en'):
    reviewed_total_data = []
    with open(place_data_path, "r") as file:
        data = json.load(file)
        for place in data:
            api_client = ApiClient(api_key=outscraper_key)
            all_responses = api_client.google_maps_reviews(
                place['place_id'],
                language=language,
                limit=100,
                reviews_limit=reviews_limit,
            )
            response = all_responses[0]
            item = dict(
                full_address=response.get('full_address', None),
                country=response.get('country', None),
                city=response.get('city', None),
                postal_code=response.get('postal_code', None),
            )
            reviews = []
            if response["reviews_data"] not in [None, []]:
                for review in response["reviews_data"]:
                    review_item = dict(
                        review_text=review.get('review_text', None),
                        review_timestamp=review.get('review_timestamp', None),
                        review_rating=review.get('review_rating', None)
                    )
                    reviews.append(review_item)
            item['reviews'] = reviews
            place.update(item)
            reviewed_total_data.append(place)

    with open(saved_reviewed_path, 'w') as f:
        json.dump(reviewed_total_data, f, ensure_ascii=False)

Leave a Comment