{"id":637,"date":"2017-04-26T08:57:54","date_gmt":"2017-04-26T00:57:54","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=637"},"modified":"2023-08-07T20:57:28","modified_gmt":"2023-08-07T12:57:28","slug":"api-article76","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/04\/26\/api-article76\/","title":{"rendered":"\u597d\u73a9\u7684API\u8c03\u7528\u4e4b&#8212;\u5929\u6c14\u9884\u62a5\u7684API\u8c03\u7528\u4e0e\u722c\u866b"},"content":{"rendered":"<p>\u597d\u73a9\u7684API\u8c03\u7528\u4e4b---\u5929\u6c14\u9884\u62a5\u7684API\u8c03\u7528\u4e0e\u722c\u866b<\/p>\n<h3>\u5e73\u65f6\u5199\u7a0b\u5e8f\u7ecf\u5e38\u9700\u8981\u7528\u5230\u4e00\u4e9b\u670d\u52a1\uff0c\u50cf\u7ffb\u8bd1\uff0c\u5929\u6c14\u9884\u62a5\uff0c\u661f\u5ea7\u4ec0\u4e48\u7684\uff0c\u6211\u4e00\u822c\u90fd\u662f\u7528Python\u5199\u4e2a\u722c\u866b\u53bb\u63d0\u4f9b\u8fd9\u4e9b\u670d\u52a1\u7684\u7f51\u7ad9\u722c\u6570\u636e\uff0c\u4f46\u662f\u6709\u4e9b\u7f51\u7ad9\u5bf9\u722c\u866b\u6709\u5f88\u591a\u9650\u5236\uff0c\u4e00\u4e9b\u5173\u952e\u5b57\u4f1a\u5b9a\u65f6\u66f4\u6539\uff0c\u5c31\u50cf\u4e2d\u56fd\u5929\u6c14\u7f51\u7ecf\u5e38\u53d8\u66f4HTML\u6807\u7b7e\u7684class\u503c\uff0c\u8fd9\u5c31\u9700\u8981\u65f6\u5e38\u7ef4\u62a4\u722c\u866b\uff0c\u800c\u805a\u5408\u6570\u636eAPI\u53ea\u5bf9\u666e\u901a\u7528\u6237\u63d0\u4f9b\u4e00\u4e2a\u514d\u8d39API\u63a5\u53e3\uff0c\u7b80\u76f4\u5783\u573e\uff0c\u800c\u7f51\u4e0a\u7684\u4e00\u4e9b\u7f51\u7ad9\u5176\u5b9e\u6709\u5f00\u653e\u7684API\u4f9b\u5f00\u53d1\u8005\u8c03\u7528\uff0c\u6240\u4ee5\u6211\u60f3\u7740\u628a\u81ea\u5df1\u53d1\u73b0\u7684\u597d\u73a9\u7684API\u548c\u81ea\u5df1\u5199\u7684\u722c\u866b\u5199\u4e2a\u535a\u5ba2\u4e13\u9898\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u6211\u4f1a\u7ee7\u7eed\u8865\u5145\u548c\u7ef4\u62a4\u3002<\/h3>\n<h2>\u7b2c\u56db\u4e2a\u4e13\u9898\u662f\u5173\u4e8e\u5929\u6c14\u9884\u62a5\u7684API\u8c03\u7528\u4e0e\u722c\u866b\uff0c\u805a\u5408\u6570\u636e\u91cc\u7684\u5929\u6c14\u9884\u62a5\u63a5\u53e3\u8fd8\u6536\u8d39\uff0c\u771f\u662flj\uff0c\u6211\u4e00\u5f00\u59cb\u7528\u722c\u866b\u722c\u4e2d\u56fd\u5929\u6c14\u7f51\u91cc\u7684\u6570\u636e\u8fdb\u884c\u5206\u6790\uff0c\u540e\u6765\u53d1\u73b0\u4e86\u548c\u98ce\u5929\u6c14\u8fd9\u4e2a\u826f\u5fc3\u7f51\u7ad9\uff0c\u4e0d\u4ec5\u63d0\u4f9b\u514d\u8d39\u7684\u63a5\u53e3\uff0c\u800c\u4e14\u5929\u6c14\u9884\u62a5\u6570\u636e\u4e5f\u5f88\u591a\u5f88\u4e30\u5bcc\u3002\u4eca\u5929\u5c31\u5199\u4e2d\u56fd\u5929\u6c14\u7f51\u7684\u722c\u866b\u548c\u548c\u98ce\u5929\u6c14\u7684api\u8c03\u7528\u3002<\/h2>\n<h2>1.\u4e2d\u56fd\u5929\u6c14\u7f51\u7684\u7f51\u5740 <a href=\"http:\/\/www.weather.com.cn\/\">http:\/\/www.weather.com.cn\/<\/a> \uff0c\u5148\u5728\u91cc\u9762\u627e\u5230\u81ea\u5df1\u7684\u57ce\u5e02\uff0c\u7136\u540e\u628a\u7f51\u5740\u590d\u5236\u4e0b\u6765\uff0c\u5c31\u50cf\u6211\u7684\u662f\u547c\u548c\u6d69\u7279\u5e02<a href=\"http:\/\/www.weather.com.cn\/weather1d\/101080101.shtml\">http:\/\/www.weather.com.cn\/weather1d\/101080101.shtml<\/a>\uff0c\u5c31\u662f\u4e0b\u56fe\u8fd9\u6837\u7684\u3002<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/web17.jpg\" width=\"1114\" height=\"645\" \/><\/p>\n<h2>\u4e0b\u9762\u662f\u6211\u722c\u866b\u7684\u4ee3\u7801\uff0c\u5c31\u4e0d\u505a\u8be6\u7ec6\u89e3\u91ca\u3002<\/h2>\n<pre><code class=\"python\">#-*- coding=utf8 -*-\nimport sys\nreload(sys)\nsys.setdefaultencoding( \"utf-8\" )\nimport urllib2\nfrom bs4 import BeautifulSoup\nimport time\ndef download(url,headers):\n    try:\n        request = urllib2.Request(url,headers=headers)\n        html = urllib2.urlopen(request).read()\n        # html = urllib2.urlopen(url).read()\n    except urllib2.URLError as e:\n        print \"error\"\n        print e.code   #\u53ef\u4ee5\u6253\u5370\u51fa\u6765\u9519\u8bef\u4ee3\u53f7\u5982404\u3002\n        print e.reason  #\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\n        html = None\n    return html\ndef save(html):\n    f = open('thefile.txt', 'w')\n    f.write(html)\n    f.close()\ndef read_file():\n    f = open('thefile.txt', 'r')\n    html = f.read()\n    f.close()\n    return html\ndef get_html(url):\n    User_agent = 'Mozilla\/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko\/20100101 Firefox\/50.0'\n    headers = {'User_agent': User_agent}\n    html = download(url, headers)\n    save(html)\ndef weather():\n    url = 'http:\/\/www.weather.com.cn\/weather\/101080101.shtml'\n    get_html(url)\n    html = read_file()\n    soup = BeautifulSoup(html)\n    text = \"\"\n    text = text + \"\u660e\u5929\u7684\u5929\u6c14\u662f\uff1a\" + soup.find('li', class_='sky skyid lv1').p.string + \"\\n\"\n    text = text + \"\u6700\u9ad8\u6e29\u5ea6\uff1a\" + soup.find('li', class_='sky skyid lv1').span.string + \"\u6700\u4f4e\u6e29\u5ea6\uff1a\" + soup.find('li',\n                                                                                                      class_='sky skyid lv1').i.string + \"\\n\"\n\n    # print soup.find('li',class_='sky skyid lv2').p.string\n    # print soup.find('li',class_='sky skyid lv2').span.string\n    # print soup.find('li',class_='sky skyid lv2').i.string\n    html = soup.find('li', class_='sky skyid lv1')\n    html2 = soup.find_all('div', class_='hide')[1]\n    soup = BeautifulSoup(str(html))\n    text = text + \"\u660e\u5929\u7684\u98ce\u662f\uff1a\" + soup.find_all('i')[1].string + '\\n'\n    # print soup.find_all('i')[1].string\n    soup = BeautifulSoup(str(html2))\n    text = text + \"\u800c\u7d2b\u5916\u7ebf\u6307\u6570\u662f\uff1a\" + soup.find_all('span')[0].string + \"  \u5efa\u8bae\uff1a\" + soup.find_all('p')[0].string + '\\n'\n    text = text + \"\u5f53\u7136\u8fd8\u6709\u611f\u5192\u6307\u6570\uff1a\" + soup.find_all('span')[1].string + \"  \u5efa\u8bae\uff1a\" + soup.find_all('p')[1].string + '\\n'\n    text = text + \"\u6700\u540e\u662f\u7a7f\u8863\u6307\u6570\uff1a\" + soup.find_all('span')[2].string + \"  \" + soup.find_all('p')[2].string + '\\n'\n     # print soup.find_all('span')[1].string\n    # print soup.find_all('span')[0].string\n    # print soup.find_all('p')[0].string\n    return text\nif __name__=='__main__':\n    weather_text = weather()\n\n<\/code><\/pre>\n<h2>2.\u6253\u5f00\u548c\u98ce\u7f51\u7ad9\uff0c\u7f51\u5740\u662f\u8fd9\u4e2a<a href=\"https:\/\/www.heweather.com\">https:\/\/www.heweather.com<\/a>\uff0c\u7136\u540e\u6ce8\u518c\u8d26\u53f7\uff0c\u627e\u5230\u81ea\u5df1\u7684KEY\uff0c\u518d\u6253\u5f00\u8fd9\u4e2aAPI\u8bf4\u660e\u3002<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/web16.jpg\" width=\"1115\" height=\"642\" \/><\/p>\n<h2>\u53ef\u4ee5\u81ea\u5df1\u53c2\u8003API\u8fd9\u51e0\u79cd\u6570\u636e\uff0c\u6211\u53ea\u4f7f\u7528\u76843-10\u5929\u6c14\u9884\u62a5\u548c\u751f\u6d3b\u6307\u6570\uff0c\u8fd8\u6709\u5929\u6c14\u56fe\u7247\u3002\u56e0\u4e3a\u548c\u98ce\u7f51\u7ad9\u8fd4\u56de\u7684\u662fjson\u683c\u5f0f\u6570\u636e\uff0c\u5982\u4e0b\u56fe\u3002<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/web13.jpg\" width=\"679\" height=\"395\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/web14.jpg\" width=\"674\" height=\"342\" \/><\/p>\n<h2>\u6211\u662f\u4f7f\u7528Python\u505a\u6570\u636e\u7684\u89e3\u6790\uff0c\u5404\u79cd\u6570\u636e\u5df2\u7ecf\u63d0\u53d6\u51fa\u6765\uff0c\u540e\u9762\u90fd\u6709\u6ce8\u91ca\uff0c\u4e0b\u9762\u662f\u4ee3\u7801<\/h2>\n<pre><code class=\"python\">#-*- coding=utf8 -*-\nimport sys\nreload(sys)\nsys.setdefaultencoding( \"utf-8\" )\nimport urllib2\nimport json\nimport re\ndef download(url):\n    html = urllib2.urlopen(url)\n    return html.read()\ndef weather():\n    # all\u5168\u90e8\u7684\u5929\u6c14\u6570\u636e\n    all_url = 'https:\/\/free-api.heweather.com\/v5\/weather?city=CN101080101&amp;key=5c043b56de9f4371b0c7f8bee8f5b75e'\n    # 3\u5929\u9884\u62a5\n    forecast_url = 'https:\/\/free-api.heweather.com\/v5\/forecast?city=CN101080101&amp;key=5c043b56de9f4371b0c7f8bee8f5b75e'\n    #\u751f\u6d3b\u6307\u6570\n    sugg_url = 'https:\/\/free-api.heweather.com\/v5\/suggestion?city=CN101080101&amp;key=5c043b56de9f4371b0c7f8bee8f5b75e'\n    # \u5929\u6c14\u56fe\u6807\n    photo_url = 'https:\/\/cdn.heweather.com\/cond_icon\/100.png'\n\n    #\u5929\u6c14\u60c5\u51b5\u7684\u5185\u5bb9\u63d0\u53d6------------\u5f00\u59cb\n    html = download(forecast_url)\n    max_tmp = re.findall('max\":\"(.*?)\"', html)[0]  #\u6700\u9ad8\u6e29\u5ea6\n    min_tmp = re.findall('min\":\"(.*?)\"', html)[0]  #\u6700\u4f4e\u6e29\u5ea6\n    photo = re.findall('code_d\":\"(.*?)\"', html)[0] #\u5929\u6c14\u56fe\u7247\n    txt_d = re.findall('txt_d\":\"(.*?)\"', html)[0] #\u5929\u6c14\u60c5\u51b5\n    dir = re.findall('dir\":\"(.*?)\"', html)[0]  # \u98ce\u5411\n    sc = re.findall('sc\":\"(.*?)\"', html)[0]  # \u98ce\u529b\n    print max_tmp,min_tmp,photo,txt_d,dir,sc\n\n    #\u751f\u6d3b\u6307\u6570\u7b49\u5185\u5bb9\u7684\u63d0\u53d6-----------\u5f00\u59cb\n    html = download(sugg_url)\n    brf = re.findall('brf\":\"(.*?)\"', html)\n    txt = re.findall('txt\":\"(.*?)\"', html)\n    comf_brf = brf[0]#\u8212\u9002\u5ea6\u6307\u6570\n    comf_txt = txt[0]\n    cw_brf = brf[1]#\u6d17\u8f66\u6307\u6570\n    cw_txt = txt[1]\n    drsg_brf = brf[2]#\u7a7f\u8863\u6307\u6570\n    drsg_txt = txt[2]\n    flu_brf = brf[3]#\u611f\u5192\u6307\u6570\n    flu_txt = txt[3]\n    sport_brf = brf[4]#\u8fd0\u52a8\u6307\u6570\n    sport_txt = txt[4]\n    trav_brf = brf[5]#\u65c5\u6e38\u6307\u6570\n    trav_txt = txt[5]\n    uv_brf = brf[6]# \u7d2b\u5916\u7ebf\u6307\u6570\n    uv_txt = txt[6]\n    print comf_brf,comf_txt\n    print cw_brf,cw_txt\n    print drsg_brf,drsg_txt\n    print flu_brf,flu_txt\n    print sport_brf,sport_txt\n    print trav_brf,trav_txt\n    print uv_brf,uv_txt\n    # brf = brf[0].decode('utf-8')\n    # dict_html = dict(result)\n    # print dict_html\n    #json\u683c\u5f0f\u65e0\u6cd5\u63d0\u53d6\n    # json_html = json.loads(html)\n    # print json_html\n    # html = str(json_html['HeWeather5'])\n    # json_html = json.loads(html)\n    # print json_html['basic']\ndef main():\n    weather()\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n<h2>\u6570\u636e\u5904\u7406\u4ee5\u540e\u7684\u6548\u679c<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/web15.jpg\" width=\"623\" height=\"248\" \/><\/p>\n<h2>\u6700\u540e\u8fd8\u6709webServer\u4e5f\u63d0\u4f9b\u670d\u52a1\uff0c\u4f46\u9700\u8981\u6ce8\u518c\uff0c\u8fd4\u56de\u7684\u6570\u636e\u4e5f\u6ca1\u6709\u548c\u98ce\u5929\u6c14\u8fd4\u56de\u7684\u6570\u636e\u4e30\u5bcc\uff0c\u6240\u4ee5\u6211\u6ca1\u4f7f\u7528\uff0c\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u53c2\u8003\u3002\u8fd9\u662f\u7f51\u5740<a href=\"http:\/\/webservice.36wu.com\/weatherService.asmx\">http:\/\/webservice.36wu.com\/weatherService.asmx<\/a><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/web18.jpg\" width=\"1366\" height=\"1363\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u73a9\u7684API\u8c03\u7528\u4e4b&#8212;\u5929\u6c14\u9884\u62a5\u7684API\u8c03\u7528\u4e0e\u722c\u866b \u5e73\u65f6\u5199\u7a0b\u5e8f\u7ecf\u5e38\u9700\u8981\u7528\u5230\u4e00\u4e9b\u670d\u52a1\uff0c\u50cf\u7ffb\u8bd1\uff0c\u5929\u6c14\u9884\u62a5\uff0c\u661f\u5ea7\u4ec0\u4e48\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/04\/26\/api-article76\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":695,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[15,18],"tags":[58,72,74,81],"class_list":["post-637","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-python","category-technique","tag-python","tag-72","tag-know","tag-reptile","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=637"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/637\/revisions"}],"predecessor-version":[{"id":1462,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/637\/revisions\/1462"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/695"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}