{"id":1104293,"date":"2025-01-08T16:19:26","date_gmt":"2025-01-08T08:19:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104293.html"},"modified":"2025-01-08T16:19:29","modified_gmt":"2025-01-08T08:19:29","slug":"python%e7%9f%a5%e9%81%93%e5%9c%b0%e5%9d%80%e5%a6%82%e4%bd%95%e5%ae%9a%e7%bb%8f%e7%ba%ac%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1104293.html","title":{"rendered":"python\u77e5\u9053\u5730\u5740\u5982\u4f55\u5b9a\u7ecf\u7eac\u5ea6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065727\/175c04bf-f382-4dc2-a187-7d022e959d11.webp\" alt=\"python\u77e5\u9053\u5730\u5740\u5982\u4f55\u5b9a\u7ecf\u7eac\u5ea6\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5730\u7406\u7f16\u7801\uff08Geocoding\uff09\u670d\u52a1\u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u7ecf\u7eac\u5ea6\u5750\u6807\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Google Maps Geocoding API\u3001OpenStreetMap\u7684Nominatim\u3001Geopy\u5e93\u7b49\u3002<\/strong> \u5176\u4e2d\uff0cGeopy\u5e93\u662f\u4e00\u4e2aPython\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u63a5\u53e3\u6765\u8bbf\u95ee\u591a\u4e2a\u5730\u7406\u7f16\u7801\u670d\u52a1\u3002\u901a\u8fc7Geopy\u5e93\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u4ee3\u7801\u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u7ecf\u7eac\u5ea6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Geopy\u5e93\u8fdb\u884c\u5730\u7406\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Geopy\u5e93\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>Geopy\u662f\u4e00\u4e2aPython\u5e93\uff0c\u7528\u4e8e\u5730\u7406\u7f16\u7801\u3001\u53cd\u5411\u5730\u7406\u7f16\u7801\u548c\u5176\u4ed6\u5730\u7406\u64cd\u4f5c\u3002\u5b83\u652f\u6301\u591a\u4e2a\u5730\u7406\u7f16\u7801\u670d\u52a1\uff0c\u5982Google Maps\u3001OpenStreetMap\u3001Bing Maps\u7b49\u3002\u4f7f\u7528Geopy\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u7ecf\u7eac\u5ea6\u5750\u6807\uff0c\u6216\u8005\u5c06\u7ecf\u7eac\u5ea6\u5750\u6807\u8f6c\u6362\u4e3a\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Geopy\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Geopy\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5148\u8fdb\u884c\u5b89\u88c5\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install geopy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Geopy\u8fdb\u884c\u5730\u7406\u7f16\u7801<\/h3>\n<\/p>\n<p><p>Geopy\u5e93\u4e2d\u5305\u542b\u4e86\u591a\u4e2a\u5730\u7406\u7f16\u7801\u670d\u52a1\u7684\u63a5\u53e3\uff0c\u4e0b\u9762\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528OpenStreetMap\u7684Nominatim\u670d\u52a1\u8fdb\u884c\u5730\u7406\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528Geopy\u8fdb\u884c\u5730\u7406\u7f16\u7801\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.geocoders import Nominatim<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u5730\u7406\u7f16\u7801\u5668\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aNominatim\u5730\u7406\u7f16\u7801\u5668\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6e\u7528\u6237\u4ee3\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">geolocator = Nominatim(user_agent=&quot;myGeocoder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8fdb\u884c\u5730\u7406\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>geocode<\/code>\u65b9\u6cd5\u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u7ecf\u7eac\u5ea6\u5750\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">address = &quot;1600 Amphitheatre Parkway, Mount<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n View, CA&quot;<\/p>\n<p>location = geolocator.geocode(address)<\/p>\n<p>print((location.latitude, location.longitude))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u5730\u5740\u201c1600 Amphitheatre Parkway, Mountain View, CA\u201d\u8f6c\u6362\u4e3a\u7ecf\u7eac\u5ea6\u5750\u6807\uff0c\u5e76\u6253\u5370\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u5730\u7406\u7f16\u7801\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u5730\u7406\u7f16\u7801\u7ed3\u679c\u5305\u542b\u4e86\u4e30\u5bcc\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5904\u7406\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u83b7\u53d6\u8be6\u7ec6\u5730\u5740\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>location<\/code>\u5bf9\u8c61\u83b7\u53d6\u8be6\u7ec6\u7684\u5730\u5740\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(location.address)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u83b7\u53d6\u7ecf\u7eac\u5ea6\u5750\u6807<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5206\u522b\u83b7\u53d6\u7eac\u5ea6\u548c\u7ecf\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">latitude = location.latitude<\/p>\n<p>longitude = location.longitude<\/p>\n<p>print(f&quot;Latitude: {latitude}, Longitude: {longitude}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u83b7\u53d6\u5176\u4ed6\u4fe1\u606f<\/h4>\n<\/p>\n<p><p><code>location.raw<\/code>\u5c5e\u6027\u5305\u542b\u4e86\u5730\u7406\u7f16\u7801\u7ed3\u679c\u7684\u539f\u59cb\u6570\u636e\uff0c\u53ef\u4ee5\u4ece\u4e2d\u63d0\u53d6\u66f4\u591a\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">raw_data = location.raw<\/p>\n<p>print(raw_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5176\u4ed6\u5730\u7406\u7f16\u7801\u670d\u52a1<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Nominatim\u4e4b\u5916\uff0cGeopy\u8fd8\u652f\u6301\u5176\u4ed6\u5730\u7406\u7f16\u7801\u670d\u52a1\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Google Maps Geocoding API\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5\u989d\u5916\u4f9d\u8d56<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Google Maps Geocoding API\u9700\u8981\u5b89\u88c5<code>requests<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.geocoders import GoogleV3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u5730\u7406\u7f16\u7801\u5668\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aGoogleV3\u5730\u7406\u7f16\u7801\u5668\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6eAPI\u5bc6\u94a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">geolocator = GoogleV3(api_key=&#39;YOUR_API_KEY&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8fdb\u884c\u5730\u7406\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>geocode<\/code>\u65b9\u6cd5\u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u7ecf\u7eac\u5ea6\u5750\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">address = &quot;1600 Amphitheatre Parkway, Mountain View, CA&quot;<\/p>\n<p>location = geolocator.geocode(address)<\/p>\n<p>print((location.latitude, location.longitude))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u5730\u7406\u7f16\u7801\u64cd\u4f5c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u9700\u8981\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u5730\u5740\u65e0\u6548<\/h4>\n<\/p>\n<p><p>\u5f53\u5730\u5740\u65e0\u6548\u65f6\uff0c<code>geocode<\/code>\u65b9\u6cd5\u8fd4\u56de<code>None<\/code>\uff0c\u9700\u8981\u8fdb\u884c\u5224\u65ad\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">location = geolocator.geocode(address)<\/p>\n<p>if location:<\/p>\n<p>    print((location.latitude, location.longitude))<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Address not found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7f51\u7edc\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5f53\u7f51\u7edc\u5f02\u5e38\u65f6\uff0c\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u9700\u8981\u8fdb\u884c\u6355\u83b7\u548c\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.exc import GeocoderTimedOut<\/p>\n<p>try:<\/p>\n<p>    location = geolocator.geocode(address, timeout=10)<\/p>\n<p>    if location:<\/p>\n<p>        print((location.latitude, location.longitude))<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Address not found&quot;)<\/p>\n<p>except GeocoderTimedOut:<\/p>\n<p>    print(&quot;Geocoding service timed out&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u53cd\u5411\u5730\u7406\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u53cd\u5411\u5730\u7406\u7f16\u7801\u662f\u5c06\u7ecf\u7eac\u5ea6\u5750\u6807\u8f6c\u6362\u4e3a\u5730\u5740\u3002Geopy\u5e93\u540c\u6837\u652f\u6301\u53cd\u5411\u5730\u7406\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528Nominatim\u8fdb\u884c\u53cd\u5411\u5730\u7406\u7f16\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">location = geolocator.reverse((37.4220656, -122.0840897))<\/p>\n<p>print(location.address)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u83b7\u53d6\u8be6\u7ec6\u5730\u5740\u4fe1\u606f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">address = location.address<\/p>\n<p>print(f&quot;Address: {address}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u83b7\u53d6\u5176\u4ed6\u4fe1\u606f<\/h4>\n<\/p>\n<p><p><code>location.raw<\/code>\u5c5e\u6027\u5305\u542b\u4e86\u53cd\u5411\u5730\u7406\u7f16\u7801\u7ed3\u679c\u7684\u539f\u59cb\u6570\u636e\uff0c\u53ef\u4ee5\u4ece\u4e2d\u63d0\u53d6\u66f4\u591a\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">raw_data = location.raw<\/p>\n<p>print(raw_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6279\u91cf\u5730\u7406\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u5927\u91cf\u5730\u5740\u8fdb\u884c\u5730\u7406\u7f16\u7801\u3002\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u548c\u6279\u91cf\u5904\u7406\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5faa\u73af\u8fdb\u884c\u5730\u7406\u7f16\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">addresses = [<\/p>\n<p>    &quot;1600 Amphitheatre Parkway, Mountain View, CA&quot;,<\/p>\n<p>    &quot;1 Infinite Loop, Cupertino, CA&quot;,<\/p>\n<p>    &quot;1601 Willow Road, Menlo Park, CA&quot;<\/p>\n<p>]<\/p>\n<p>for address in addresses:<\/p>\n<p>    location = geolocator.geocode(address)<\/p>\n<p>    if location:<\/p>\n<p>        print(f&quot;Address: {address}, Latitude: {location.latitude}, Longitude: {location.longitude}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Address: {address}, not found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u6279\u91cf\u5904\u7406\u8fdb\u884c\u5730\u7406\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u5730\u5740\u5217\u8868\u5206\u6279\u5904\u7406\uff0c\u51cf\u5c11\u5bf9\u5730\u7406\u7f16\u7801\u670d\u52a1\u7684\u8bf7\u6c42\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import islice<\/p>\n<p>def batch(iterable, n=1):<\/p>\n<p>    it = iter(iterable)<\/p>\n<p>    while True:<\/p>\n<p>        chunk = list(islice(it, n))<\/p>\n<p>        if not chunk:<\/p>\n<p>            break<\/p>\n<p>        yield chunk<\/p>\n<p>addresses = [<\/p>\n<p>    &quot;1600 Amphitheatre Parkway, Mountain View, CA&quot;,<\/p>\n<p>    &quot;1 Infinite Loop, Cupertino, CA&quot;,<\/p>\n<p>    &quot;1601 Willow Road, Menlo Park, CA&quot;,<\/p>\n<p>    # \u6dfb\u52a0\u66f4\u591a\u5730\u5740<\/p>\n<p>]<\/p>\n<p>for batch_addresses in batch(addresses, 3):<\/p>\n<p>    for address in batch_addresses:<\/p>\n<p>        location = geolocator.geocode(address)<\/p>\n<p>        if location:<\/p>\n<p>            print(f&quot;Address: {address}, Latitude: {location.latitude}, Longitude: {location.longitude}&quot;)<\/p>\n<p>        else:<\/p>\n<p>            print(f&quot;Address: {address}, not found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5730\u7406\u7f16\u7801\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>\u4f7f\u7528\u7f13\u5b58<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u51cf\u5c11\u5bf9\u5730\u7406\u7f16\u7801\u670d\u52a1\u7684\u8bf7\u6c42\u6b21\u6570\u548c\u63d0\u9ad8\u6548\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u3002\u5728Geopy\u5e93\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Memory<\/code>\u6216<code>File<\/code>\u7f13\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.geocoders import Nominatim<\/p>\n<p>from geopy.extra.rate_limiter import RateLimiter<\/p>\n<p>from cachetools import TTLCache<\/p>\n<p>geolocator = Nominatim(user_agent=&quot;myGeocoder&quot;)<\/p>\n<p>geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)<\/p>\n<p>cache = TTLCache(maxsize=100, ttl=300)<\/p>\n<p>def cached_geocode(address):<\/p>\n<p>    if address in cache:<\/p>\n<p>        return cache[address]<\/p>\n<p>    location = geocode(address)<\/p>\n<p>    cache[address] = location<\/p>\n<p>    return location<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u907f\u514d\u9891\u7e41\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u5730\u7406\u7f16\u7801\u670d\u52a1\u901a\u5e38\u5bf9\u8bf7\u6c42\u9891\u7387\u6709\u9650\u5236\uff0c\u907f\u514d\u9891\u7e41\u8bf7\u6c42\u53ef\u4ee5\u51cf\u5c11\u88ab\u9650\u5236\u7684\u98ce\u9669\u3002\u53ef\u4ee5\u4f7f\u7528<code>RateLimiter<\/code>\u6765\u63a7\u5236\u8bf7\u6c42\u9891\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.geocoders import Nominatim<\/p>\n<p>from geopy.extra.rate_limiter import RateLimiter<\/p>\n<p>geolocator = Nominatim(user_agent=&quot;myGeocoder&quot;)<\/p>\n<p>geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5730\u7406\u7f16\u7801\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u9700\u8981\u8fdb\u884c\u5904\u7406\u3002\u5e38\u89c1\u7684\u5f02\u5e38\u5305\u62ec\u8d85\u65f6\u3001\u65e0\u6548\u5730\u5740\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.exc import GeocoderTimedOut, GeocoderServiceError<\/p>\n<p>try:<\/p>\n<p>    location = geocode(address)<\/p>\n<p>    if location:<\/p>\n<p>        print((location.latitude, location.longitude))<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Address not found&quot;)<\/p>\n<p>except (GeocoderTimedOut, GeocoderServiceError) as e:<\/p>\n<p>    print(f&quot;Geocoding error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u548cGeopy\u5e93\u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u7ecf\u7eac\u5ea6\u5750\u6807\u3002\u901a\u8fc7\u5b89\u88c5Geopy\u5e93\uff0c\u5e76\u4f7f\u7528Nominatim\u6216\u5176\u4ed6\u5730\u7406\u7f16\u7801\u670d\u52a1\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5730\u7406\u7f16\u7801\u548c\u53cd\u5411\u5730\u7406\u7f16\u7801\u3002\u540c\u65f6\uff0c\u672c\u6587\u8fd8\u4ecb\u7ecd\u4e86\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3001\u6279\u91cf\u5730\u7406\u7f16\u7801\u3001\u4f7f\u7528\u7f13\u5b58\u548c\u907f\u514d\u9891\u7e41\u8bf7\u6c42\u7b49\u6700\u4f73\u5b9e\u8df5\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u5730\u7406\u7f16\u7801\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u7ecf\u7eac\u5ea6\uff1f<\/strong><br \/>\u8981\u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u7ecf\u7eac\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u5730\u7406\u7f16\u7801\u5e93\uff0c\u5982Geopy\u3002\u4f60\u53ea\u9700\u5b89\u88c5\u8be5\u5e93\uff0c\u7136\u540e\u4f7f\u7528\u5176\u63d0\u4f9b\u7684\u529f\u80fd\u5c06\u5730\u5740\u8f93\u5165\u5e76\u83b7\u53d6\u5bf9\u5e94\u7684\u7ecf\u7eac\u5ea6\u4fe1\u606f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from geopy.geocoders import Nominatim\n\ngeolocator = Nominatim(user_agent=&quot;geoapiExercises&quot;)\nlocation = geolocator.geocode(&quot;\u4f60\u7684\u5730\u5740&quot;)\nprint((location.latitude, location.longitude))\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u8fc7\u7a0b\u7b80\u5355\u6613\u884c\uff0c\u80fd\u591f\u5feb\u901f\u83b7\u53d6\u6240\u9700\u7684\u5730\u7406\u5750\u6807\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u5730\u5740\u5230\u7ecf\u7eac\u5ea6\u8f6c\u6362\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u5730\u5740\u5230\u7ecf\u7eac\u5ea6\u8f6c\u6362\u65f6\uff0c\u786e\u4fdd\u8f93\u5165\u7684\u5730\u5740\u683c\u5f0f\u6b63\u786e\uff0c\u6e05\u6670\u4e14\u5b8c\u6574\u3002\u6b64\u5916\uff0c\u4e0d\u540c\u7684\u5730\u7406\u7f16\u7801\u670d\u52a1\u53ef\u80fd\u8fd4\u56de\u7684\u7ed3\u679c\u6709\u6240\u4e0d\u540c\uff0c\u56e0\u6b64\u9009\u62e9\u4e00\u4e2a\u53ef\u9760\u7684\u670d\u52a1\u975e\u5e38\u91cd\u8981\u3002Geopy\u652f\u6301\u591a\u79cd\u5730\u7406\u7f16\u7801\u670d\u52a1\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5730\u5740\u8f6c\u6362\u4e2d\u7684\u5f02\u5e38\u6216\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fdb\u884c\u5730\u5740\u5230\u7ecf\u7eac\u5ea6\u8f6c\u6362\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u5730\u5740\u65e0\u6548\u6216\u670d\u52a1\u4e0d\u53ef\u7528\u3002\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u6349\u8fd9\u4e9b\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4f7f\u7528try-except\u8bed\u53e5\u6765\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u53cd\u9988\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    location = geolocator.geocode(&quot;\u4f60\u7684\u5730\u5740&quot;)\n    if location:\n        print((location.latitude, location.longitude))\n    else:\n        print(&quot;\u672a\u627e\u5230\u8be5\u5730\u5740\u7684\u7ecf\u7eac\u5ea6\u4fe1\u606f\u3002&quot;)\nexcept Exception as e:\n    print(f&quot;\u53d1\u751f\u9519\u8bef: {e}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u786e\u4fdd\u7528\u6237\u83b7\u5f97\u826f\u597d\u7684\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5730\u7406\u7f16\u7801\uff08Geocoding\uff09\u670d\u52a1\u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u7ecf\u7eac\u5ea6\u5750\u6807\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Googl [&hellip;]","protected":false},"author":3,"featured_media":1104307,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104293"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1104293"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104293\/revisions"}],"predecessor-version":[{"id":1104312,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104293\/revisions\/1104312"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1104307"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}