从谷歌地图API获取评论

27
5个回答

44

另一种更近期的方法:

https://maps.googleapis.com/maps/api/place/details/json?placeid={place_id}&key={api_key}

响应内容:

{
  "html_attributions": [],
  "result": {
    ...
    "rating": 4.6,
    "reviews": [
      {
        "author_name": "John Smith",
        "author_url": "https://www.google.com/maps/contrib/106615704148318066456/reviews",
        "language": "en",
        "profile_photo_url": "https://lh4.googleusercontent.com/-2t1b0vo3t-Y/AAAAAAAAAAI/AAAAAAAAAHA/0TUB0z30s-U/s150-c0x00000000-cc-rp-mo/photo.jpg",
        "rating": 5,
        "relative_time_description": "in the last week",
        "text": "Great time! 5 stars!",
        "time": 1508340655
      }
    ]
  }
}

评论仅限于最新的5条。


2
谢谢,@abbas udin提到的旧方法不起作用,但这个可以返回评论。 - Wang'l Pakhrin
我遇到了以下错误:跨源资源共享(CORS)策略阻止了从 'https://saifkart.com' 访问 'https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJkQYP8U6EXjkRShBlLAg2uqs&key=apikey' 的XMLHttpRequest。请求的资源上没有 'Access-Control-Allow-Origin' 头信息。 - user1881820
1
这个可以工作,但只能获取最近的5条评论,为了获取更多的评论,我不得不使用一个叫做wextractor.com的外部服务。 - Pesto D
3
它返回的是5条评论而不是最新的5条评论! - pseudozach
你知道有没有一种方法可以获取用户在多个地方的评论吗?也许可以使用用户API? - Craig Lambie
你如何让它在本地主机上运行?使用Laragon,我有一个example.com.test网站,通过hosts指向127.0.0.1。然后$url = "https://maps.googleapis.com/maps/api/place/details/json?placeid={$place_id}&key={$key}"; $result = json_decode(file_get_contents($url), true);返回array ( 'error_message' => 'This API project is not authorized to use this API.', 'html_attributions' => array ( ), 'status' => 'REQUEST_DENIED', ) - s3c

5

要获取谷歌评论,您需要该地点的参考ID。为了获得此参考键,您可以使用谷歌地点搜索API请求。

https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+bangalore&sensor=true&key=AddYourOwnKeyHere

它的响应将包含参考ID,您可以在请求中使用该ID。

    <PlaceSearchResponse>
    <status>OK</status>
    <result>
    <name>Koshy's Restaurant</name>
    <type>bar</type>
    <type>restaurant</type>
    <type>food</type>
    <type>establishment</type>
    <formatted_address>
    39, St Marks Road,Shivajinagar,Bangalore, Karnataka, 560001, India
    </formatted_address>
    <geometry>
    <rating>3.7</rating>
    <icon>
    http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png
    </icon>
    **<reference>**
    CnRwAAAA1z8aCeII_F2wIVcCnDVPQHQi5zdd-3FsDl6Xhb_16OGrILvvvI4X4M8bFk2U8YvuDCKcFBn_a2rjvYDtvUZJrHykDAntE48L5UX9hUy71Z4n80cO7ve_JXww6zUkoisfFnu6jEHcnKeeTUE42PCA4BIQGhGz0VrXWbADarhKwCQnKhoUOR-Xa9R6Skl0TZmOI4seqt8rO8I
    **</reference>**
    <id>2730db556ca6707ef517e5c165adda05d2395b90</id>
    <opening_hours>
    <open_now>true</open_now>
    </opening_hours>
    <html_attribution>
    <a href="https://plus.google.com/116912222767108657277">Ujaval Gandhi</a>
    </html_attribution>
    </photo>
    </result>

3
$reqUri = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?key='.YOURSERVERKEY;
$reqUri .= '&sensor=false&radius=500';
$reqUri .= '&location=38.908310,-104.784035&name='.urlencode(LOCATION NAME);
$reqUri .= '&keyword='.urlencode(WEBSITE PHONE);

我已经用PHP完成了,现在只需像这样调用URL,您就可以得到带有参考键的原始结果。 然后进行解析:
$data = cURL($reqUri);
$data = json_decode($data);
echo $data ->results[0]->reference;

希望这能对您有所帮助。

***注意:location=38.908310,-104.784035 这个变量不是自动的,您必须拥有它。


最多5条评论,根据文档: * reviews[] 一个JSON数组,最多包括五条评论。如果在Place Details请求中指定了语言参数,则Places服务会偏向于显示使用该语言撰写的评论。 - SHG
我尝试了你的API调用,但它没有返回任何评论的JSON。 - Wang'l Pakhrin

1
使用Python和Google Places API,可以按以下方式检索企业详细信息和评论(最多5个评论):
api = GooglePlaces("Your API key")

places = api.search_places_by_coordinate("40.819057,-73.914048", "100", "restaurant")

for place in places:
    details = api.get_place_setails(place['place_id'], fields)
    try:
        website = details['result']['website']
    except KeyError:
        website = ""

    try:
        name = details['result']['name']
    except KeyError:
        name = ""

    try:
        address = details['result']['formatted_address']
    except KeyError:
        address = ""

    try:
        phone_number = details['result']['international_phone_number']
    except KeyError:
        phone_number = ""

    try:
        reviews = details['result']['reviews']
    except KeyError:
        reviews = []
    print("===================PLACE===================")
    print("Name:", name)
    print("Website:", website)
    print("Address:", address)
    print("Phone Number", phone_number)
    print("==================REVIEWS==================")
    for review in reviews:
        author_name = review['author_name']
        rating = review['rating']
        text = review['text']
        time = review['relative_time_description']
        profile_photo = review['profile_photo_url']
        print("Author Name:", author_name)
        print("Rating:", rating)
        print("Text:", text)
        print("Time:", time)
        print("Profile photo:", profile_photo)
        print("-----------------------------------------")

关于此代码和Google Places API的更多细节,您可以查看此教程:https://python.gotrained.com/google-places-api-extracting-location-data-reviews/


你好,有没有办法获取超过5条评论(即使我付费)?我已经启用了计费,但似乎找不到如何做到这一点...可能不得不使用Selenium.. :-) - tezzaaa
@tezzaaa,付款选项似乎与请求次数有关(而非评论)。https://developers.google.com/places/web-service/usage-and-billing 因此,显然对你的问题的直接答案是“否”-在这里检查答案:https://dev59.com/UlkS5IYBdhLWcg3w-Kzl - GoTrained

0

我在本地主机上遇到了问题。我添加了我的example.com.test域名,但当然我无法验证它,因为它无法从外部访问(除了ngrok变体)。

我在GitHub上发现了一个惊人的脏技巧:gaffling/PHP-Grab-Google-Reviews

对我来说非常有效,除了我不得不将/* CHECK SORT */行更改为if (isset($option['sort_by_reating_best_1']) and $option['sort_by_reating_best_1'] == true),并且我还通过可选的第二个函数参数将foreach限制为仅5条评论。

完全不需要API_KEY


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接