GeoNames Home | Postal Codes | Download / Webservice | About 

GeoNames Forum
  [Search] Search   [Recent Topics] Recent Topics   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Handling Pagination in Code  XML
Forum Index -> Discussion of GeoNames Toponyms
Author Message
philipcraigt



Joined: 12/08/2024 06:41:42
Messages: 1
Offline

python
Sao chép mã
import requests

def fetch_children(geoname_id, username, max_rows=10):
url = "http://api.geonames.org/childrenJSON"
results = []
start = 0

while True:
response = requests.get(url, params={
'geonameId': geoname_id,
'username': username,
'start': start,
'maxRows': max_rows
})
data = response.json()
children = data.get('geonames', [])
results.extend(children)

if len(children) < max_rows:
break

start += max_rows

return results

# Example usage
username = 'YOUR_USERNAME'
geoname_id = '123456'
children = fetch_children(geoname_id, username)
print(children)
 
Forum Index -> Discussion of GeoNames Toponyms
Go to:   
Powered by JForum 2.1.5 © JForum Team