{"id":713,"date":"2018-10-31T22:00:53","date_gmt":"2018-10-31T14:00:53","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=713"},"modified":"2022-02-16T22:10:41","modified_gmt":"2022-02-16T14:10:41","slug":"2019ironman-flask-restful-lxml-requests","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2018\/10\/31\/2019ironman-flask-restful-lxml-requests\/","title":{"rendered":"\u7b2c\u5341\u5c46\u9435\u4eba\u8cfd flask-restful DAY30-\u641e\u61c2 Requests \u8207 lxml"},"content":{"rendered":"<h1>\u7b46\u8005\u600e\u9ebc\u958b\u59cbflask-restful\u7684<\/h1>\n<p>30\u5929\u7684flask-restful\u7d42\u65bc\u8981\u544a\u4e00\u6bb5\u843d\u4e86\uff0c\u4eca\u5929\u4f86\u8ac7\u8ac7\u7b46\u8005\u600e\u9ebc\u63a5\u89f8flask-restful<!--more--><\/p>\n<p>\u7531\u65bc\u7b46\u8005\u81ea\u5df1\u6709\u5728\u8a18\u9304\u91d1\u878d\u8cc7\u8a0a\uff0c\u5982\u5982\u80a1\u50f9\u3001\u532f\u7387\u7b49\uff0c\u4f46\u662f\u90fd\u662f\u624b\u52d5\u67e5\u8a62\u7db2\u7ad9\u5728\u4e00\u7b46\u4e00\u7b46\u7684\u7d00\u9304\u5728txt\u5167\uff0c\u7531\u65bc\u8fd1\u5e74\u4f86\u81ea\u52d5\u5316\u76db\u884c\uff0c\u6240\u4ee5\u7b46\u8005\u60f3\u8aaa\u4e5f\u5c31\u5beb\u500b\u5de5\u5177\u4f86\u8a18\u9304\u3002<\/p>\n<p>\u800c\u70ba\u4ec0\u9ebc\u6311\u4e0aflask-restful\u5462\uff1f\u5c31\u5982\u540c\u7b2c\u4e00\u5929\u544a\u8a34\u5927\u5bb6\u7684\uff0cflask-restful\u7684\u7c21\u6f54\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c08\u6ce8\u5728\u5546\u696d\u908f\u8f2f\u4e0a\u7684\u958b\u767c\uff0c\u5c31\u9019\u6a23\u5c55\u958bflask-restful\u4e4b\u65c5\u3002<\/p>\n<h1>\u4ecb\u7d39requests<\/h1>\n<p>\u82b1\u4e8630\u5929\u4ecb\u7d39flask-restful\u5f8c\u518d\u4f86\u4ecb\u7d39\u5982\u4f55\u5c07\u81ea\u52d5\u5316\u722c\u7db2\u7ad9\u6574\u9032\u5c08\u6848\u4e2d\uff0c\u6240\u4ee5\u9996\u5148\u5148\u4ecb\u7d39\u722c\u7db2\u9801\u7684\u5de5\u5177<a href=\"http:\/\/docs.python-requests.org\/en\/master\/\" rel=\"nofollow noopener\" target=\"_blank\">requests<\/a>\u3002<\/p>\n<h2>\u5b89\u88ddrequests<\/h2>\n<p>\u9996\u5148\u4e0d\u5916\u4e4e\u5c31\u662f\u5b89\u88dd\u51fd\u5f0f\u5eab\u4e86\uff0c\u9019\u88e1\u5c31\u4e0d\u5ee2\u8a71\u4e86\uff0c\u82e5\u662f\u4e0d\u77e5\u9053\u600e\u9ebc\u5b89\u88dd\u7684\u8acb\u770b\u4ee5\u4e0b\u4f8b\u5b50\u3002<\/p>\n<pre><code class=\"language-bash\">$ pip install requests<\/code><\/pre>\n<h2>\u4f7f\u7528requests<\/h2>\n<p>\u5b89\u88dd\u5b8c\u5c31\u53ef\u4ee5\u5c0f\u8a66\u8eab\u624b\u4e86\uff0c\u6240\u4ee5\u5148\u8a66\u8a66\u4ee5\u4e0b\u4f8b\u5b50\u3002<\/p>\n<pre><code class=\"language-python\">        response = requests.get(\n            &#039;http:\/\/www.twse.com.tw\/exchangeReport&#039;\n            &#039;\/STOCK_DAY?response=html&amp;date=&#039;\n            + date + &quot;&amp;stockNo=0050&quot;)<\/code><\/pre>\n<p>\u9019\u662f\u7b46\u8005\u900f\u904e\u53f0\u7063\u8b49\u5238\u4ea4\u6613\u6240\u516c\u958b\u8cc7\u8a0a\u7db2\u7ad9\u4e0a\u6293\u53d6\u53f0\u7063\u4e94\u53410050\u7684\u76f8\u95dc\u8cc7\u6599\u7db2\u9801\uff0c\u900f\u904erequests.get\u53ef\u4ee5\u53d6\u5f97\u7db2\u9801\u76f8\u95dc\u5167\u5bb9\u56de\u50b3\u56de\u4f86\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u5c31\u662f\u8981\u89e3\u6790response.content\u7684\u5167\u5bb9\u4e86\u3002<\/p>\n<h2>\u89e3\u6790xml<\/h2>\n<p>\u56e0\u70ba\u56de\u4f86\u7684\u8cc7\u8a0a\u4e00\u5b9a\u662fhtml\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u8981\u4f86\u89e3\u6790\u53d6\u5f97\u7684\u8cc7\u6599\uff0c\u4e26\u53d6\u5f97\u6211\u5011\u6240\u9700\u8981\u7684\u8cc7\u8a0a\u3002<\/p>\n<blockquote>\n<p>\u9664\u975e\u8b80\u8005\u722c\u7684\u662fapi\uff0c\u4e0d\u904eapi\u61c9\u8a72\u4e0d\u9700\u8981\u7279\u5225\u53bb\u722c\u3002<\/p>\n<\/blockquote>\n<h1>\u4ecb\u7d39lxml<\/h1>\n<p>\u9019\u88e1\u7b46\u8005\u7528\u4f86\u89e3\u6790\u53d6\u5f97\u8cc7\u6599\u7684\u5de5\u5177\u662f<a href=\"https:\/\/lxml.de\" rel=\"nofollow noopener\" target=\"_blank\">xml<\/a>\uff0c\u6240\u4ee5\u7b2c\u4e00\u6b65\u4e0d\u5916\u4e4e\u662f\u5b89\u88ddlxml\u4e86\u3002<\/p>\n<h2>\u5b89\u88ddlxml<\/h2>\n<p>\u9019\u88e1\u4e0d\u5916\u4e4e\u5c31\u662f\u900f\u904epip\u5b89\u88dd\u4e86\uff0c\u5982\u679c\u4e0d\u719f\u7684\u8b80\u8005\u8acb\u770b\u4ee5\u4e0b\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-bash\">$ pip install lxml<\/code><\/pre>\n<h2>\u4ecb\u7d39xpath<\/h2>\n<p>\u9019\u90e8\u5206\u4e3b\u8981\u662f\u5c07\u4e0a\u4e00\u500b\u6b65\u9a5f\u6240\u53d6\u5f97\u7684response.content\u653e\u5230lxml\u5167\u89e3\u6790\u7522\u751fxml\u7684\u7d50\u69cb\u5373\u53ef\uff0c\u4f8b\u5982\u4ee5\u4e0b\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-python\">        html = etree.HTML(response.content)\n        stockList = []\n        for item in html.xpath(&#039;\/html\/body\/div\/table\/tbody\/tr&#039;):\n            # item[0].text-&gt;\u65e5\u671f\n            # item[3].text-&gt;\u958b\u76e4\u50f9\n            # item[4].text-&gt;\u6700\u9ad8\u50f9\n            # item[5].text-&gt;\u6700\u4f4e\u50f9\n            # item[6].text-&gt;\u6536\u76e4\u50f9\n            # item[7].text-&gt;\u6f32\u8dcc\u50f9\u5dee\n            # item[1].text-&gt;\u6210\u4ea4\u80a1\u6578\n            # item[2].text-&gt;\u6210\u4ea4\u91d1\u984d\n            # \u65e5\u671f\u6c11\u570b\u8f49\u897f\u5143\n            year = int(item[0].text.split(&#039;\/&#039;)[0]) + 1911\n            month = int(item[0].text.split(&#039;\/&#039;)[1])\n            day = int(item[0].text.split(&#039;\/&#039;)[2])\n            itemDate = datetime.datetime(year, month, day)\n            if (maxDate &lt; itemDate):\n                stockList.append(\n                    StockModel(itemDate.strftime(&#039;%Y%m%d&#039;), float(item[3].text),\n                               float(item[4].text), float(item[5].text),\n                               float(item[6].text), float(item[7].text),\n                               round(\n                                   int(item[1].text.replace(&#039;,&#039;, &#039;&#039;)) \/ 1000),\n                               round(int(item[2].text.replace(&#039;,&#039;, &#039;&#039;)) \/ 1000)))\n        if (len(stockList) &gt; 0):\n            StockModel.save_list_to_db(stockList)<\/code><\/pre>\n<p>\u96d6\u7136\u6211\u5011\u53d6\u7684xml\u7684\u7d50\u69cb\u5c0d\u6211\u5011\u5e6b\u52a9\u5f88\u5927\uff0c\u4f46\u662f\u6211\u5011\u53ea\u662f\u9700\u8981\u5176\u4e2d\u7684\u4e00\u4e9b\u8cc7\u8a0a\uff0c\u6240\u4ee5\u9019\u90e8\u4efd\u900f\u904e<a href=\"https:\/\/zh.wikipedia.org\/wiki\/XPath\" rel=\"nofollow noopener\" target=\"_blank\">xpath<\/a>\u76f4\u63a5\u53d6\u7684\u6211\u5011\u60f3\u8981\u7684\u8cc7\u6599\uff0c\u4f8b\u5982\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u6211\u5011\u5728\u4e4e\u7684\u662f\u80a1\u50f9\u76f8\u95dc\u8cc7\u8a0a\uff0c\u800c\u5176\u8cc7\u6599\u662f\u5229\u7528table\u4f86\u5b58\u653e\u7684\uff0c\u6240\u4ee5\u6211\u5011\u5c31\u900f\u904expath<code><code>\/html\/body\/div\/table\/tbody\/tr<\/code><\/code>\u53d6\u7684\u6211\u5011\u6240\u8981\u7684\u6bcf\u500bcolumn\u7684\u5167\u5bb9\uff0c\u518d\u4f86\u4e0a\u8ff0\u4f8b\u5b50\u662f\u5c07\u5176\u5167\u5bb9\u6574\u7406\u6700\u5f8c\u5b58\u653e\u5230sqlite\u5167\u3002<\/p>\n<h2>\u5be6\u969b\u89e3\u6790\u7db2\u9801\u5167\u5bb9<\/h2>\n<p>\u7531\u65bc\u6bcf\u4e00\u500b\u7db2\u7ad9\u7684\u7d50\u69cb\u90fd\u4e0d\u4e00\u6a23\uff0c\u6240\u4ee5\u60f3\u8981\u64f7\u53d6\u6240\u9700\u7684\u8cc7\u6599\u4e0d\u5916\u4e4e\u5c31\u662f\u8981\u53bb\u4e86\u89e3\u8a72\u7db2\u7ad9\u7684\u7d50\u69cb\uff0c\u77e5\u9053\u60f3\u8981\u7684\u8cc7\u6599\u653e\u7f6e\u65bc\u90a3\u88e1\u4e4b\u5f8c\u518d\u900f\u904expath\u9078\u53d6\u60f3\u8981\u7684\u8cc7\u6599\u5f8c\u6574\u7406\u5b58\u653e\u5230\u8cc7\u6599\u5eab\u5167\uff0c\u63a5\u4e0b\u4f86\u518d\u900f\u904eflask-restful\u5be6\u4f5cGET\u7684\u65b9\u6cd5\u4f86\u8fd4\u9084\u8cc7\u6599\u7d66client\u7aef\uff0c\u5982\u6b64\u5c31\u53ef\u4ee5\u81ea\u5df1\u64c1\u6709\u4e00\u500b\u80a1\u50f9api\u7db2\u7ad9\u4e86\u3002<\/p>\n<h1>\u5c0f\u7d50<\/h1>\n<p>30\u5929\u4e00\u8f49\u773c\u5c31\u904e\u4e86\uff0c\u611f\u8b1d\u5927\u5bb6\u7684\u652f\u6301\u8207\u966a\u4f34\u8b93\u7b46\u8005\u53ef\u4ee5\u5b8c\u6210\u9019\u4e9b\u5167\u5bb9\uff0c\u8b1d\u8b1d\u5404\u4f4d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b46\u8005\u600e\u9ebc\u958b\u59cbflask-restful\u7684 30\u5929\u7684flask-restful\u7d42\u65bc\u8981\u544a\u4e00\u6bb5\u843d\u4e86\uff0c\u4eca\u5929\u4f86\u8ac7\u8ac7\u7b46\u8005\u600e &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2018\/10\/31\/2019ironman-flask-restful-lxml-requests\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008\u7b2c\u5341\u5c46\u9435\u4eba\u8cfd flask-restful DAY30-\u641e\u61c2 Requests \u8207 lxml\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[162,4,5,23,3,21],"class_list":["post-713","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2018ironman","tag-flask","tag-flask-restful","tag-lxml","tag-python","tag-requests"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=713"}],"version-history":[{"count":10,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/713\/revisions"}],"predecessor-version":[{"id":2739,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/713\/revisions\/2739"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}