{"id":633,"date":"2017-04-25T20:59:52","date_gmt":"2017-04-25T12:59:52","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=633"},"modified":"2023-08-07T20:57:28","modified_gmt":"2023-08-07T12:57:28","slug":"api-article74","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/04\/25\/api-article74\/","title":{"rendered":"\u597d\u73a9\u7684API\u8c03\u7528\u4e4b&#8212;\u661f\u5ea7\u8fd0\u52bf\u7684API\u4e0e\u722c\u866b\u722c\u53d6"},"content":{"rendered":"<p>\u597d\u73a9\u7684API\u8c03\u7528\u4e4b---\u661f\u5ea7\u8fd0\u52bfAPI\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\u4e8c\u4e2a\u4e13\u9898\u662f\u5173\u4e8e\u661f\u5ea7\u8fd0\u52bf\u67e5\u8be2\u7684\uff0c\u5e73\u65f6\u65e0\u804a\u7684\u65f6\u5019\u4f1a\u7528\u5230\u8fd9\u4e2a\uff0c\u5e73\u65f6\u653e\u5728\u7f51\u7ad9\u91cc\u663e\u793a\u4e00\u4e0b\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u4ee5\u524d\u6211\u662f\u76f4\u63a5Python\u722c\u661f\u5ea7\u5c4b\u7f51\u7ad9\u91cc\u7684\u6570\u636e\u53bb\u5206\u6790\u7684\uff0c\u540e\u6765\u53d1\u73b0\u805a\u5408\u6570\u636e\u63d0\u4f9b\u8fd9\u4e2aAPI\uff0c\u800c\u4e14\u8c03\u7528\u7684\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff0c\u4eca\u5929\u5c31\u628a\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5199\u51fa\u6765\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u81ea\u5df1\u53c2\u8003\u7740\u505a\u3002<\/h2>\n<h2>1. \u8fd9\u662f\u661f\u5ea7\u5c4b\u7684\u7f51\u7ad9<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/web6.jpg\" width=\"1081\" height=\"646\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u7136\u540e\u7528Python\u5199\u4e2a\u722c\u866b\u76f4\u63a5\u722c\u5c31\u53ef\u4ee5\u4e86\uff0c\u6211\u76f4\u63a5\u653e\u51fa\u4ee3\u7801\uff0c\u4e0d\u505a\u89e3\u91ca\u4e86<\/h3>\n<pre><code class=\"python\">#-*- coding=utf8 -*-\nimport sys\nimport MySQLdb\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 constellation():\n    url = 'http:\/\/www.xzw.com\/fortune\/pisces\/1.html'\n    get_html(url)\n    html = read_file()\n    soup = BeautifulSoup(html)\n    # print soup.find_all('dl')\n    html2 = soup.find('div', class_='c_cont')\n    html2 = str(html2)\n    soup = BeautifulSoup(html2)\n    text = '\u53cc\u9c7c\u5ea7\u660e\u65e5\u8fd0\u52bf\uff1a' + '\\n'\n    text = text + '\u6574\u4f53\u8fd0\u52bf\uff1a' + soup.find_all('span')[0].string + '\\n'\n    text = text + '\u4e8b\u4e1a\u5b66\u4e1a\uff1a' + soup.find_all('span')[2].string + '\\n'\n    text = text + '\u5065\u5eb7\u8fd0\u52bf\uff1a' + soup.find_all('span')[4].string + '\\n'\n    return text\n    # html = urllib2.urlopen(url)\n    # print html\nif __name__=='__main__':\n    weather_text = weather()\n\n<\/code><\/pre>\n<h2>2.\u5148\u5728\u805a\u5408\u6570\u636e\u91cc\u6ce8\u518c\uff0c\u7136\u540e\u518d\u63d0\u4ea4\u8eab\u4efd\u8bc1\u7167\u7247\u8fdb\u884c\u4e2a\u4eba\u8ba4\u8bc1\uff0c\u5f88\u70e6\u7684\uff0c\u6700\u540e\u5728\u514d\u8d39\u6570\u636e\u91cc\u627e\u5230\u661f\u5ea7\u8fd0\u52bf\uff0c\u5c31\u662f\u8fd9\u6837\u7684<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/web7.jpg\" width=\"998\" height=\"642\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u7136\u540e\u70b9\u51fb\u7533\u8bf7\u6570\u636e\uff0c\u518d\u8fdb\u884c\u4e00\u904d\u9a8c\u8bc1\uff0c\u4e0d\u8fc7\u5f88\u5feb\u5c31\u901a\u8fc7\u4e86\uff0c\u5728\u63a7\u5236\u53f0\u627e\u7684\u81ea\u5df1\u7684key\uff0c\u6211\u662f\u4f7f\u7528Python\u6765\u5904\u7406\u6570\u636e\u7684\uff0c\u4e0b\u9762\u662f\u4ee3\u7801\uff0c\u628akey\u76f4\u63a5\u6362\u6210\u4f60\u7684\u5c31\u53ef\u4ee5\uff0c\u81f3\u4e8e\u7c7b\u578b\uff0c\u6839\u636e\u9700\u8981\u5207\u6362today\uff0ctomorrow\uff0cweek\uff0cyear\u7b49\u3002<\/h3>\n<pre><code class=\"Python\">#-*- coding=utf8 -*-\nimport sys\nreload(sys)\nsys.setdefaultencoding( \"utf-8\" )\nimport urllib2\nimport json\nfrom bs4 import BeautifulSoup\ndef download(url):\n    html = urllib2.urlopen(url)\n    return html.read()\n#\u661f\u5ea7\u8fd0\u52bfapi\u7684\u8c03\u53d6\ndef constellation():\n    today_url = 'http:\/\/web.juhe.cn:8080\/constellation\/getAll?consName=\u53cc\u9c7c\u5ea7&amp;type=today&amp;key='\n    tomorrow_url = 'http:\/\/web.juhe.cn:8080\/constellation\/getAll?consName=\u53cc\u9c7c\u5ea7&amp;type=tomorrow&amp;key='\n    html = download(tomorrow_url)\n    json_html = json.loads(html)\n    text = ''\n    text = text + str(json_html['name']) + ' ' + str(json_html['datetime']) + ' \u660e\u65e5\u8fd0\u52bf \\n';\n    text = text + '\u7efc\u5408\u6307\u6570\uff1a' + str(json_html['all']) + '\\n'\n    text = text + '\u5e78\u8fd0\u8272\uff1a' + str(json_html['color']) + '\\n'\n    text = text + '\u5065\u5eb7\u6307\u6570\uff1a' + str(json_html['health']) + '\\n'\n    text = text + '\u7231\u60c5\u6307\u6570\uff1a' + str(json_html['love']) + '\\n'\n    text = text + '\u8d22\u8fd0\u6307\u6570\uff1a' + str(json_html['money']) + '\\n'\n    text = text + '\u5e78\u8fd0\u6570\u5b57\uff1a' + str(json_html['number']) + '\\n'\n    text = text + '\u660e\u65e5\u6982\u8ff0\uff1a' + str(json_html['summary']) + '\\n'\n    print text\n    return text\ndef main():\n    text = constellation()\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u73a9\u7684API\u8c03\u7528\u4e4b&#8212;\u661f\u5ea7\u8fd0\u52bfAPI\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\u7684\uff0c\u6211\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/04\/25\/api-article74\/\">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":[72,74],"class_list":["post-633","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-python","category-technique","tag-72","tag-know","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/633","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=633"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"predecessor-version":[{"id":1464,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/633\/revisions\/1464"}],"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=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}