{"id":269,"date":"2020-08-13T01:27:08","date_gmt":"2020-08-12T17:27:08","guid":{"rendered":"http:\/\/blog.cyasylum.top\/?p=269"},"modified":"2020-08-13T01:27:08","modified_gmt":"2020-08-12T17:27:08","slug":"%e5%85%b3%e4%ba%8e%e7%ae%80%e6%98%93%e7%88%ac%e8%99%ab%e5%92%8c%e5%9c%b0%e5%9b%be%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"http:\/\/blog.cyasylum.top\/index.php\/2020\/08\/13\/%e5%85%b3%e4%ba%8e%e7%ae%80%e6%98%93%e7%88%ac%e8%99%ab%e5%92%8c%e5%9c%b0%e5%9b%be%e8%b0%83%e7%94%a8\/","title":{"rendered":"\u5173\u4e8e\u7b80\u6613\u722c\u866b\u548c\u5730\u56fe\u8c03\u7528"},"content":{"rendered":"\n<p>\u8fd9\u662f\u4e00\u6b21\u5f88\u7b80\u5355\u7684\u5173\u4e8e\u722c\u866b\u7684\u5c1d\u8bd5\uff0c\u6bd5\u7adf\u5728\u5927\u591a\u6570\u9700\u8981\u4f7f\u7528\u722c\u866b\u7684\u60c5\u51b5\u4e0b\uff0c\u7f51\u9875\u5e76\u4e0d\u4f1a\u4ee5api\u7684\u5f62\u5f0f\u8fd4\u56de\u7ed9\u4f60\u5982\u6b64\u5de5\u6574\u7684\u6570\u636e\u3002\u7a0d\u5fae\u8bb0\u5f55\u4e00\u4e0b\u5427\uff0c\u4ee5\u5907\u540e\u7eed\u4f7f\u7528<\/p>\n\n\n\n<p>\u867d\u7136\u6211\u89c9\u5f97\u6211\u4e0d\u4f1a\u518d\u7528\u767e\u5ea6\u5730\u56fe\u4e86\u5c31\u662f<\/p>\n\n\n\n<p>\u767e\u5ea6\u5730\u56fe\u5f88\u9ebb\u70e6\u5c31\u5728\uff0c\u60f3\u8fdb\u884c\u8def\u5f84\u89c4\u5212\u53ea\u80fd\u4f7f\u7528\u5730\u70b9\u7684\u7ecf\u7eac\u5ea6\uff0c\u636e\u540c\u5b66\u8bf4\u9ad8\u5fb7\u5730\u56fe\u7684api\u5c31\u4e0d\u5b58\u5728\u8fd9\u4e2a\u5751\u7239\u95ee\u9898\uff0c\u867d\u7136\u4e5f\u53ef\u4ee5\u79f0\u4e4b\u4e3a\u4e25\u8c28\u5566\uff0c\u4f46\u662f\u8fd8\u662f\u4ee4\u4eba\u76f8\u5f53\u4e0d\u723d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\nimport json\n\nobj = \"\u75be\u63a7\u4e2d\u5fc3\"\ncity = \"\u676d\u5dde\"\nak = \"******\"\n\nreq_url = \"http:\/\/api.map.baidu.com\/place\/v2\/search?query={obj}&amp;region={city}&amp;output=json&amp;ak={ak}\".format(obj=obj, city=city, ak=ak)\n\nresponse = requests.get(req_url)\nans = response.json()\nwith open(r\"5 - I'M VIRU\\Codes\\loc.json\", 'w', encoding='utf-8') as file:\n    file.write(json.dumps(ans, indent=2, ensure_ascii=False))<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u76f8\u5f53\u7b80\u6613\u7684\u8bbf\u95ee\u8bf7\u6c42\u5e76\u83b7\u53d6json\u6587\u4ef6\u7684\u6784\u9020\uff0c\u901a\u8fc7\u5411\u767e\u5ea6api\u8bbf\u95ee\u676d\u5dde\u5e02\u75be\u63a7\u4e2d\u5fc3\u7684\u4fe1\u606f\uff0c\u83b7\u53d6\u76f8\u5173\u7684\u6240\u6709\u8d44\u6599\uff0c\u5e76\u50a8\u5b58\u5728json\u6587\u4ef6\u4e2d\u3002\u5176\u683c\u5f0f\u5927\u6982\u662f\u8fd9\u6837\u7684<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"status\": 0,\n  \"message\": \"ok\",\n  \"results\": &#91;\n    {\n      \"name\": \"\u676d\u5dde\u5e02\u75be\u75c5\u9884\u9632\u63a7\u5236\u4e2d\u5fc3\",\n      \"location\": {\n        \"lat\": 30.313231,\n        \"lng\": 120.234488\n      },\n      \"address\": \"\u7b15\u6865\u9547\u660e\u77f3\u8def568\u53f7\u676d\u5dde\u5e02\u516c\u5171\u536b\u751f\u4e2d\u5fc3\u5185\",\n      \"province\": \"\u6d59\u6c5f\u7701\",\n      \"city\": \"\u676d\u5dde\u5e02\",\n      \"area\": \"\u6c5f\u5e72\u533a\",\n      \"street_id\": \"a5fad7a038e5e19a714b871d\",\n      \"detail\": 1,\n      \"uid\": \"a5fad7a038e5e19a714b871d\"\n    }\n  ]\n}<\/code><\/pre>\n\n\n\n<p>\u8fd8\u7b97\u4e0d\u9519\uff0c\u7b2c\u4e00\u4e2a\u76ee\u6807\u5c31\u662f\u6211\u4eec\u9700\u8981\u7684\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u4f7f\u7528\u4e86\u6bd4\u8f83\u62c9\u80ef\u7684\u624b\u6bb5\u628a\u4e00\u4e32json\u6587\u4ef6\u901a\u8fc7\u7f51\u9875\u8f6c\u5316\u6210\u4e86csv\u6587\u4ef6\uff0c\u7136\u540e\u6574\u7406\u83b7\u5f97\u4e86\u75be\u63a7\u4e2d\u5fc3\u4e0e\u5730\u7406\u5750\u6807location\u7684\u5bf9\u5e94\u5173\u7cfb\u3002\u4f7f\u7528\u7684\u7f51\u9875\u662f\u8fd9\u4e00\u5bb6<a href=\"http:\/\/www.convertcsv.com\/json-to-csv.htm\" target=\"_blank\"  rel=\"nofollow\" >JSON To CSV Converter<\/a>\uff0c\u8fd8\u7b97\u5f88\u8212\u670d\u4e86\uff0c\u975e\u5e38\u503c\u5f97\u63a8\u4ecb\uff0c\u6574\u7406\u5f97\u8868\u683c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lid    lat lng\n\u536b\u751f\u5065\u5eb7\u59d4\u5458\u4f1a    30.253522   120.216045\n\u676d\u5dde\u5e02\u75be\u63a7\u4e2d\u5fc3    30.313231   120.234488\n\u200b``````\n\u4f59\u676d\u533a\u75be\u63a7\u4e2d\u5fc3    30.402791   120.290841\n\u897f\u6e56\u533a\u75be\u63a7\u4e2d\u5fc3    30.288965   120.070272<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u4ee3\u7801\u6784\u9020\u5730\u70b9\u95f4\u7684\u8def\u7531\u77e9\u9635\uff0c\u5e76\u914d\u7f6e\u4fe1\u606f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\nimport csv\nimport re\nimport time\nimport json\nfrom urllib.request import urlopen\nimport urllib\n\npathlist = r\"5 - I'M VIRU\\Codes\\list.csv\"\nresultlist = r\"5 - I'M VIRU\\Codes\\result.csv\"\n\n# \u9009\u53d6\u5750\u6807\u683c\u5f0f,bd09ll(\u767e\u5ea6\u7ecf\u7eac\u5ea6\u5750\u6807)\ncod = r\"&amp;coord_type=bd09ll\"\n\n# \u4f60\u7684AK\nAK = '******'\n\n# \u8bfb\u53d6\u521d\u59cb\u77e9\u9635\ninit_list = pd.read_csv(pathlist, sep='\\s', encoding='utf-8')\ninit_list.head()\n\ntarget_list = &#91;]\n\n# \u904d\u5386\u884c\uff0c\u6784\u5efa\u8def\u7531\u77e9\u9635\nfor _, row1 in init_list.iterrows():\n    lid1 = row1&#91;\"lid\"]&#91;0]\n    lat1 = row1&#91;\"lat\"]\n    lng1 = row1&#91;\"lng\"]\n    for _, row2 in init_list.iterrows():\n        lid2 = row2&#91;\"lid\"]&#91;0]\n        lat2 = row2&#91;\"lat\"]\n        lng2 = row2&#91;\"lng\"]\n        target_list.append(&#91;str(lid1)+'2'+str(lid2), lat1, lng1, lat2, lng2])\n\ntarget_df = pd.DataFrame.from_records(\n    target_list, columns=&#91;'id', 'olat', 'olng', 'dlat', 'dlng'])\nprint(target_df)\n\nresult_list = &#91;]\n\nfor i in range(len(target_df)):\n    olat = target_df.at&#91;i, 'olat']\n    olng = target_df.at&#91;i, 'olng']\n    dlat = target_df.at&#91;i, 'dlat']\n    dlng = target_df.at&#91;i, 'dlng']\n\n    # \u7f16\u64b0req\u7533\u8bf7\u7f51\u5740\n    req_url = r\"http:\/\/api.map.baidu.com\/routematrix\/v2\/driving?output=json&amp;origins={0},{1}&amp;destinations={2},{3}&amp;{4}&amp;tactics=11&amp;ak={4}\".format(\n        olat, olng, dlat, dlng, AK)\n\n    result_drive = json.loads(urlopen(req_url).read())  # json\u8f6cdict\n\n    # \u8ddd\u79bb\/\u8017\u65f6\n    distance = result_drive&#91;'result']&#91;0]&#91;'distance']&#91;'value']\n    times = result_drive&#91;'result']&#91;0]&#91;'duration']&#91;'value']\n\n    result_list.append(&#91;target_df.at&#91;i, 'id'], distance, times])\n\n# \u5199\u6587\u4ef6\nresult_pd = pd.DataFrame.from_records(\n    result_list, columns=&#91;'id', 'dis', 'time'])\n\nresult_pd.to_csv(resultlist)<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u5c31\u80fd\u83b7\u5f97\u4e00\u4e2a\u8def\u7531\u77e9\u9635\u7684\u5217\u5566\uff0c\u63a5\u4e0b\u6765\u8fd8\u53ef\u4ee5\u518d\u5bf9\u884c\u5217\u8fdb\u884c\u5904\u7406\u4ee5\u65b9\u4fbf\u8c03\u7528\uff0c\u4f46\u662f\u90a3\u662f\u660e\u5929\u6c42\u89e3\u65f6\u7684\u4e8b\u60c5\u4e86\uff08x<\/p>\n\n\n\n<p>\u8bdd\u8bf4\u6211\u4e0d\u662f\u9000\u6570\u6a21\u4e86\u5417\uff0c\u4e3a\u5565\u8fd8\u8981\u641e\u8fd9\u5e7a\u86fe\u5b50<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u6b21\u5f88\u7b80\u5355\u7684\u5173\u4e8e\u722c\u866b\u7684\u5c1d\u8bd5\uff0c\u6bd5\u7adf\u5728\u5927\u591a\u6570\u9700\u8981\u4f7f\u7528\u722c\u866b\u7684\u60c5\u51b5\u4e0b\uff0c\u7f51\u9875\u5e76\u4e0d\u4f1a\u4ee5api\u7684\u5f62\u5f0f\u8fd4\u56de\u7ed9\u4f60\u5982\u6b64\u5de5\u6574\u7684\u6570\u636e\u3002\u7a0d\u5fae\u8bb0\u5f55\u4e00\u4e0b\u5427\uff0c &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[15,17],"tags":[16,50,49],"class_list":["post-269","post","type-post","status-publish","format-standard","hentry","category-15","category-17","tag-python","tag-50","tag-49"],"_links":{"self":[{"href":"http:\/\/blog.cyasylum.top\/index.php\/wp-json\/wp\/v2\/posts\/269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.cyasylum.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.cyasylum.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.cyasylum.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.cyasylum.top\/index.php\/wp-json\/wp\/v2\/comments?post=269"}],"version-history":[{"count":1,"href":"http:\/\/blog.cyasylum.top\/index.php\/wp-json\/wp\/v2\/posts\/269\/revisions"}],"predecessor-version":[{"id":270,"href":"http:\/\/blog.cyasylum.top\/index.php\/wp-json\/wp\/v2\/posts\/269\/revisions\/270"}],"wp:attachment":[{"href":"http:\/\/blog.cyasylum.top\/index.php\/wp-json\/wp\/v2\/media?parent=269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.cyasylum.top\/index.php\/wp-json\/wp\/v2\/categories?post=269"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.cyasylum.top\/index.php\/wp-json\/wp\/v2\/tags?post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}