API запросов Python - отображается только один результат, ожидаются два - PullRequest
0 голосов
/ 11 мая 2018

Я работаю над скриптом Python, который извлекает широту и долготу из API карт Google (Geocode).Вот мой код:

import requests
import time
import MysqlConnect
import addslashes



GOOGLE_MAPS_API_URL = 'https://maps.googleapis.com/maps/api/geocode/json?'

#Selecting datas from DB 
MysqlConnect.curs.execute("SELECT * FROM entreprises")
res_a=MysqlConnect.curs.fetchall()

count=0
for row in res_a :

    params = {
    'address': row[1], 
    'sensor': 'false',
    'region': 'fr',
    #'key' : 'AIzaSyA-lKrpRzcNzr2KSReq_UAq3niN01CBPdQ'
    # The script works without API key 


    }

    # Do the request and get the response data
    req = requests.get(GOOGLE_MAPS_API_URL, params=params)
    res = req.json()

    if res['status']=="OVER_QUERY_LIMIT" or res['status']=="ZERO_RESULTS" :
        while res['status']=="OVER_QUERY_LIMIT" or res['status']=="ZERO_RESULTS" :
            req = requests.get(GOOGLE_MAPS_API_URL, params=params)
            res = req.json()
            print("Statu : "+res['status'])

    if res['status']=="REQUEST_DENIED" : 
        print ("Requête rejetée ...")
        pass
    elif res['status']=="INVALID_REQUEST":
        print ("Requête incorrecte ...")
        pass
    elif res['status']=="UNKNOWN_ERROR":
        print ("Erreur serveur ...")
        pass   




    result = res['results'][0]
    lat = result['geometry']['location']['lat']
    lng = result['geometry']['location']['lng']
    count=count+1

    #Inserting datas in Mysql database 
    RS_replace=addslashes.addslashes(row[0])
    VL_replace=addslashes.addslashes(row[1])
    MysqlConnect.curs.execute("INSERT INTO `temp`  VALUES ('%s','%s','%s','%f','%d','%f','%f')" % (RS_replace,VL_replace,row[2],row[3],row[4],lat,lng))
    MysqlConnect.cnt.commit()

MysqlConnect.cnt.close()
print ("Valeurs trouvées : %d"% count)

Моя проблема в том, что, когда я выполняю запрос вручную (из моего навигатора chrome), я получаю 2 результата, используя этот URL: https://maps.googleapis.com/maps/api/geocode/json?address=LES+ARCS&sensor=false&region=fr.

->

    {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Bourg-Saint-Maurice",
               "short_name" : "Bourg-Saint-Maurice",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Savoie",
               "short_name" : "Savoie",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Auvergne-Rhône-Alpes",
               "short_name" : "Auvergne-Rhône-Alpes",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "France",
               "short_name" : "FR",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "73700",
               "short_name" : "73700",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "73700 Bourg-Saint-Maurice, France",
         "geometry" : {
            "location" : {
               "lat" : 45.5722296,
               "lng" : 6.8296421
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 45.5735764302915,
                  "lng" : 6.831917
               },
               "southwest" : {
                  "lat" : 45.5708784697085,
                  "lng" : 6.8277368
               }
            }
         },
         "place_id" : "ChIJgwxVkQ9viUcRwq995YhoKn8",
         "types" : [ "establishment", "point_of_interest", "travel_agency" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Les Arcs",
               "short_name" : "Les Arcs",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Var",
               "short_name" : "Var",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Provence-Alpes-Côte d'Azur",
               "short_name" : "Provence-Alpes-Côte d'Azur",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "France",
               "short_name" : "FR",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "83460",
               "short_name" : "83460",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "83460 Les Arcs, France",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 43.501788,
                  "lng" : 6.538443099999999
               },
               "southwest" : {
                  "lat" : 43.40484499999999,
                  "lng" : 6.4338681
               }
            },
            "location" : {
               "lat" : 43.462876,
               "lng" : 6.478446000000001
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 43.501788,
                  "lng" : 6.538443099999999
               },
               "southwest" : {
                  "lat" : 43.40484499999999,
                  "lng" : 6.4338681
               }
            }
         },
         "place_id" : "ChIJtccPrPmtzhIRXIJGDS20oXI",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

Затем, когда я выполняю свой скрипт на python, я получаю только 1 результат, хотя скрипт генерирует тот же URL-адрес ...

Любая помощь будет оценена!

Спасибо, Антуан

...