{"id":375,"date":"2021-08-23T09:03:00","date_gmt":"2021-08-23T09:03:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=375"},"modified":"2021-08-23T09:03:00","modified_gmt":"2021-08-23T09:03:00","slug":"375","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/08\/23\/375\/","title":{"rendered":"Pytyhon \u4f7f\u7528\u767e\u5ea6\u5730\u56feAPI \u8fdb\u884c IP\u666e\u901a\u5b9a\u4f4d\u548c\u5730\u56fe\u663e\u793a"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/3976009608.png\" alt=\"123.png\" title=\"123.png\"><\/p>\n<h2>\u6f14\u793a\u7f51\u5740:  <a href=\"https:\/\/www.lyvba.com\/ip\/\">https:\/\/www.lyvba.com\/ip\/<\/a><\/h2>\n<hr \/>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/650223153.webp\" alt=\"ip2bdgps.webp\" title=\"ip2bdgps.webp\"><\/p>\n<ul>\n<li>\u8fd9\u65f6\u6211\u642d\u5efa\u548c\u90e8\u7f72\u597d\u7684\u5b9e\u9645\u4f7f\u7528\u6f14\u793a\uff0c\u8c03\u7528 \u767e\u5ea6IP\u5b9a\u4f4d\u5728\u670d\u52a1\u7aef\u4f1a\u88abban\uff0c\u6240\u4ee5\u5c31\u4f7f\u7528\u57ce\u5e02\u540d\u8c03\u7528\u5730\u56fe \u548c \u83b7\u5f97json\u5728\u672c\u5730\u4f7f\u7528<\/li>\n<\/ul>\n<h3>\u767b\u9646\u767e\u5ea6\u5730\u56fe\u5f00\u653e\u5e73\u53f0\uff0c\u3010\u5e94\u7528\u7ba1\u7406\u3011-\u3010\u6211\u7684\u5e94\u7528\u3011:\u521b\u5efa\u5e94\u7528\u751f\u6210 \uff08AK\u7801\uff09<\/h3>\n<ul>\n<li>URL: <a href=\"https:\/\/lbsyun.baidu.com\/apiconsole\/key#\/home\">https:\/\/lbsyun.baidu.com\/apiconsole\/key#\/home<\/a><\/li>\n<\/ul>\n<h3>Web\u670d\u52a1API  <a href=\"https:\/\/lbsyun.baidu.com\/index.php?title=webapi\/ip-api\">\u666e\u901aIP\u5b9a\u4f4d\u6587\u6863<\/a><\/h3>\n<ul>\n<li>\n<p>\u8bf7\u6c42URL<\/p>\n<pre><code>https:\/\/api.map.baidu.com\/location\/ip?ak=\u60a8\u7684AK&amp;ip=\u60a8\u7684IP&amp;coor=bd09ll \/\/HTTPS\u534f\u8bae<\/code><\/pre>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u7ed3\u679c\uff08JSON\u683c\u5f0f\uff09\uff1a<\/p>\n<pre><code>{\n  address: \"CN|\u5317\u4eac|\u5317\u4eac|None|CHINANET|1|None\",    #\u8be6\u7ec6\u5730\u5740\u4fe1\u606f\n  content:    #\u7ed3\u6784\u4fe1\u606f\n  {\n      address: \"\u5317\u4eac\u5e02\",    #\u7b80\u8981\u5730\u5740\u4fe1\u606f\n      address_detail:    #\u7ed3\u6784\u5316\u5730\u5740\u4fe1\u606f\n      {\n          city: \"\u5317\u4eac\u5e02\",    #\u57ce\u5e02\n          city_code: 131,    #\u767e\u5ea6\u57ce\u5e02\u4ee3\u7801\n          province: \"\u5317\u4eac\u5e02\",    #\u7701\u4efd\n      },\n      point:    #\u5f53\u524d\u57ce\u5e02\u4e2d\u5fc3\u70b9\n      {\n          x: \"116.39564504\",    #\u5f53\u524d\u57ce\u5e02\u4e2d\u5fc3\u70b9\u7ecf\u5ea6\n          y: \"39.92998578\"    #\u5f53\u524d\u57ce\u5e02\u4e2d\u5fc3\u70b9\u7eac\u5ea6\n      }\n  },\n  status: 0    #\u7ed3\u679c\u72b6\u6001\u8fd4\u56de\u7801\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3><code>ip2bdgps.py<\/code><\/h3>\n<pre><code>import requests\nimport json\n\nurl = 'https:\/\/api.map.baidu.com\/location\/ip?ak=\u60a8\u7684AK&amp;ip=\u60a8\u7684IP&amp;coor=bd09ll'\nr = requests.get(url)\njson_str = r.text\ndata = json.loads(json_str)\nprint(data)<\/code><\/pre>\n<h3><code>ip2bdgps<\/code> \u51fd\u6570\u7528\u6765\u8fc7\u7684 bdgps x,y \u5750\u6807\u548c\u72b6\u6001\u7801<\/h3>\n<pre><code>import requests\ndef ip2bdgps(ip):\n    url = 'https:\/\/api.map.baidu.com\/location\/ip?ak={{ \u60a8\u7684AK }}&amp;ip=' + ip + '&amp;coor=bd09ll'\n    r = requests.get(url=url, headers=headers)\n    data = r.json()\n    if data['status'] != 0 :\n        return  (116.39564504, 39.92998578 , data['status'])    # \u67e5\u4e0d\u5230\u8fd4\u56de \u5317\u4eac x,y\n    else:\n        x = data['content']['point']['x']\n        y = data['content']['point']['y']\n    return  (x, y, data['status'], data)  # \u4e3a\u4e86\u8c03\u8bd5 \u628ajson\u6570\u636e\n\nip = '114.114.114.114'\nbdgps = ip2bdgps(ip)\nprint(bdgps)       # \u8fd4\u56de ('116.41338370', '39.91092455', 0,  ... data\u7684json\u6570\u636e )<\/code><\/pre>\n<h2>\u767e\u5ea6\u5730\u56fe\u7684 <a href=\"https:\/\/lbsyun.baidu.com\/index.php?title=jspopular3.0\/guide\/helloworld\">\u201cHello, World\u201d<\/a><\/h2>\n<p>\u5f00\u59cb\u5b66\u4e60\u767e\u5ea6\u5730\u56feAPI\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u3002\u8be5\u793a\u4f8b\u5e2e\u52a9\u60a8\u5feb\u901f\u4e86\u89e3\u767e\u5ea6\u5730\u56feAPI\u7684\u57fa\u672c\u4f7f\u7528\uff0c\u5feb\u901f\u521b\u5efa\u4e00\u5f20\u201c\u6211\u7684\u5730\u56fe\u201d\u3002<\/p>\n<p>\u5982\u4e0b\u4ee3\u7801\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u5b8c\u6574\u7684\u5168\u5c4f\u5c55\u793a\u7684\u5730\u56feDemo\uff1a<\/p>\n<pre><code>\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" \/&gt;\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/&gt;\n&lt;title&gt;Hello, World&lt;\/title&gt;\n&lt;style type=\"text\/css\"&gt;\nhtml{height:100%}\nbody{height:100%;margin:0px;padding:0px}\n#container{height:100%}\n&lt;\/style&gt;\n&lt;script type=\"text\/javascript\" src=\"https:\/\/api.map.baidu.com\/api?v=3.0&amp;ak=\u60a8\u7684\u5bc6\u94a5\"&gt;\n&lt;\/script&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n&lt;div id=\"container\"&gt;&lt;\/div&gt;\n&lt;script type=\"text\/javascript\"&gt;\nvar map = new BMap.Map(\"container\");\n\/\/ \u521b\u5efa\u5730\u56fe\u5b9e\u4f8b\nvar point = new BMap.Point(116.404, 39.915);\n\/\/ \u521b\u5efa\u70b9\u5750\u6807\nmap.centerAndZoom(point, 15);\n\/\/ \u521d\u59cb\u5316\u5730\u56fe\uff0c\u8bbe\u7f6e\u4e2d\u5fc3\u70b9\u5750\u6807\u548c\u5730\u56fe\u7ea7\u522b\nmap.enableScrollWheelZoom(true);     \/\/\u5f00\u542f\u9f20\u6807\u6eda\u8f6e\u7f29\u653e\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h3>\u9002\u5e94\u79fb\u52a8\u7aef\u9875\u9762\u5c55\u793a<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u6dfb\u52a0\u4e00\u4e2ameta\u6807\u7b7e\uff0c\u4ee5\u4fbf\u4f7f\u60a8\u7684\u9875\u9762\u66f4\u597d\u7684\u5728\u79fb\u52a8\u5e73\u53f0\u4e0a\u5c55\u793a\u3002<\/p>\n<pre><code>&lt;meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" \/&gt;\n<\/code><\/pre>\n<h3>\u8bbe\u7f6e\u5bb9\u5668\u6837\u5f0f<\/h3>\n<p>\u8bbe\u7f6e\u5bb9\u5668\u6837\u5f0f\u5927\u5c0f\uff0c\u4f7f\u5730\u56fe\u5145\u6ee1\u6574\u4e2a\u6d4f\u89c8\u5668\u7a97\u53e3\uff1a<\/p>\n<pre><code>&lt;style type=\"text\/css\"&gt;\n    html{height:100%}\n    body{height:100%;margin:0px;padding:0px}\n    #container{height:100%}\n&lt;\/style&gt;\n<\/code><\/pre>\n<h3>\u5f15\u7528\u767e\u5ea6\u5730\u56feAPI\u6587\u4ef6<\/h3>\n<pre><code>&lt;script type=\"text\/javascript\" src=\"https:\/\/api.map.baidu.com\/api?v=3.0&amp;ak=\u60a8\u7684\u5bc6\u94a5\"&gt;&lt;\/script&gt;\n<\/code><\/pre>\n<h3>\u521b\u5efa\u5730\u56fe\u5bb9\u5668\u5143\u7d20<\/h3>\n<p>\u5730\u56fe\u9700\u8981\u4e00\u4e2aHTML\u5143\u7d20\u4f5c\u4e3a\u5bb9\u5668\uff0c\u8fd9\u6837\u624d\u80fd\u5c55\u73b0\u5230\u9875\u9762\u4e0a\u3002\u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2adiv\u5143\u7d20\u3002<\/p>\n<pre><code>&lt;div id=\"container\"&gt;&lt;\/div&gt;\n<\/code><\/pre>\n<h3>\u521b\u5efa\u5730\u56fe\u5b9e\u4f8b<\/h3>\n<p>\u4f4d\u4e8eBMap\u547d\u540d\u7a7a\u95f4\u4e0b\u7684Map\u7c7b\u8868\u793a\u5730\u56fe\uff0c\u901a\u8fc7new\u64cd\u4f5c\u7b26\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5730\u56fe\u5b9e\u4f8b\u3002\u5176\u53c2\u6570\u53ef\u4ee5\u662f\u5143\u7d20id\u4e5f\u53ef\u4ee5\u662f\u5143\u7d20\u5bf9\u8c61\u3002<\/p>\n<pre><code>var map = new BMap.Map(\"container\");\n<\/code><\/pre>\n<h3>\u8bbe\u7f6e\u4e2d\u5fc3\u70b9\u5750\u6807<\/h3>\n<p>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528BMap\u547d\u540d\u7a7a\u95f4\u4e0b\u7684Point\u7c7b\u6765\u521b\u5efa\u4e00\u4e2a\u5750\u6807\u70b9\u3002Point\u7c7b\u63cf\u8ff0\u4e86\u4e00\u4e2a\u5730\u7406\u5750\u6807\u70b9\uff0c\u5176\u4e2d116.404\u8868\u793a\u7ecf\u5ea6\uff0c39.915\u8868\u793a\u7eac\u5ea6\u3002\uff08\u4e3a\u5929\u5b89\u95e8\u5750\u6807\uff09<\/p>\n<pre><code>var point = new BMap.Point(116.404, 39.915);\n\n<\/code><\/pre>\n<h3>\u5730\u56fe\u521d\u59cb\u5316\uff0c\u540c\u65f6\u8bbe\u7f6e\u5730\u56fe\u5c55\u793a\u7ea7\u522b<\/h3>\n<p>\u5728\u521b\u5efa\u5730\u56fe\u5b9e\u4f8b\u540e\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff0cBMap.Map.centerAndZoom()\u65b9\u6cd5\u8981\u6c42\u8bbe\u7f6e\u4e2d\u5fc3\u70b9\u5750\u6807\u548c\u5730\u56fe\u7ea7\u522b\u3002 \u5730\u56fe\u5fc5\u987b\u7ecf\u8fc7\u521d\u59cb\u5316\u624d\u53ef\u4ee5\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<pre><code>map.centerAndZoom(point, 15);\n\n<\/code><\/pre>\n<h3>\u5730\u56fe\u4e2d\u4f7f\u7528\u9f20\u6807\u6eda\u8f6e\u63a7\u5236\u7f29\u653e<\/h3>\n<pre><code>map.enableScrollWheelZoom(true);     \/\/\u5f00\u542f\u9f20\u6807\u6eda\u8f6e\u7f29\u653e\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6f14\u793a\u7f51\u5740: https:\/\/www.lyvba.com\/ip\/ \u8fd9\u65f6\u6211\u642d\u5efa\u548c\u90e8\u7f72\u597d\u7684\u5b9e\u9645\u4f7f\u7528\u6f14\u793a\uff0c\u8c03 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[45],"class_list":["post-375","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=375"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/375\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}