Searching online from text file with python checking if the names are cities in a specific country Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Searching online from text file with python checking if the names are cities in a specific country without wasting too much if your time.

The question is published on by Tutorial Guruji team.

So I have a large list of geographical locations that may be cities, counties, etc, in a particular country. For example country: Turkey and list of names is: Erzurum, Eskisehir, etc.

I am wondering if I can put this list into a text file and use Python to check these names online to check what geographical entity they are instead of just Googling each term individually. How would I do that?

Answer

You can also use google geocoding api. Here is an example:

a.txt:

erzurum
istanbul
turkey
chicago
united states
india
kayseri
spain

Here is the code:

import urllib2
import json
import time

def getEntity( entityText ):
    url = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s' % urllib2.quote(entityText)

    response = urllib2.urlopen(url)
    jsonaddress = json.loads(response.read())

    time.sleep(0.2)

    if jsonaddress['status'] == 'OK':
        return jsonaddress['results'][0]['types'][0]
    else:
        return None

with open('a.txt') as f:
    for line in f:
        entityText = line.strip()
        entity = getEntity( entityText ) 
        print entityText, entity

Output:

erzurum locality
istanbul locality
turkey country
chicago locality
united states country
india country
kayseri locality
spain country
We are here to answer your question about Searching online from text file with python checking if the names are cities in a specific country - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji