簡體   English   中英

為什么只返回一個結果

[英]Why is only one result being returned

Python 和 vsCode,使用 google maps API 我從 inte.net 上復制了一個程序來查找我所在地區的所有企業,但返回的數據似乎不正確。

我首先嘗試按關鍵字搜索,這返回了很多結果,但不是我想要的業務類型。 我刪除了這個變量,而是使用了一個opennow變量。 這只返回一個結果,這是我搜索過的城鎮,而不是企業。 你能看一下代碼,看看我有沒有做錯嗎?

API

map_client = googlemaps.Client(API_KEY)

location = (54.970121, -2.101585)
distance = (1)
business_list= []

response = map_client.places_nearby(
    location=location,
    radius=distance,
)

business_list.extend(response.get('results'))
next_page_token = response.get('next_page_token')

while next_page_token:
    time.sleep(2)
    response = map_client.places_nearby(
        location=location,
        radius=distance,
        opennow=True,
        page_token=next_page_token
    )
    business_list.extend(response.get('results'))
    next_page_token = response.get('next_page_token')

df = pd.DataFrame(business_list)
df['url'] = 'www.google.com/maps/place/?q=place_id:' + df['place_id']
df.to_excel('Toby Buisness.xlsx', index=False)`

非常感謝

您的附近搜索半徑太小,無法搜索任何其他內容

我希望您花時間閱讀Places API 文檔以及Python Google 地圖客戶端庫,以便更好地理解您復制的代碼。

現在它只返回一個結果的原因是因為你有這個: distance = (1) 此變量在您的響應中用作map_client.places_nearby上參數radius的值。

如果您閱讀Python Client Library ,它會說:

radius (int) – 偏移結果的距離(以米為單位)。

查看您的代碼,這意味着您的搜索半徑僅為1 meter 這解釋了為什么你只返回一個結果,因為除了 1 米范圍內的地方外,范圍內沒有其他地方,如果你沒有指定任何type ,當然它會返回它所在的附近的名稱。在您的情況下,Hexham UK。

所以我嘗試了你的代碼並使用了1000 meters的半徑並得到了超過 1 個結果。 這是示例代碼:

import googlemaps

map_client = googlemaps.Client('YOUR_API_KEY_HERE')

location = '54.970121, -2.101585'
distance = 1000
business_list = []

response = map_client.places_nearby(
    location=location,
    radius=distance,
    type='restaurant'
)

business_list.extend(response.get('results'))

print(len(business_list))

就像我之前說的,請閱讀文檔,因為您使用的opennow參數無效,正確的參數是open_now 您也可以嘗試使用我在示例中使用的type參數來搜索特定結果。

以下是您可以在附近搜索中使用的類型列表的鏈接: 表 1 地點類型

最后,請確保您的用例在他們的服務條款范圍內(在抓取/緩存 Google 地圖數據的情況下)以避免您的應用程序將來出現問題。 由於我不是法律專家,我建議您花時間閱讀這些鏈接中的條款: 3.2.3 Restrictions Against Misusing the Services / Places API Specific Terms

我希望這有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM