from geopy.geocoders import Nominatim def find_province(city): """ :param city: "福州" :return: “福建” """ try: geolocator = Nominatim(user_agent='myuseragent') location = geolocator.geocode(city) province = re.findall(b'\w+\xe7\x9c\x81'.decode('utf-8'), str(location)) return province[0].replace("省","") except: return city if __name__ == '__main__': print(find_province("福州"))
PS: pip3 install geopy