{"id":1595,"date":"2015-08-03T18:49:55","date_gmt":"2015-08-03T10:49:55","guid":{"rendered":"http:\/\/boweihe.me\/?p=1595"},"modified":"2015-08-03T18:49:55","modified_gmt":"2015-08-03T10:49:55","slug":"%e5%ae%9c%e9%80%81%e5%b0%8f%e7%88%ac%e8%99%ab-%e6%96%bd%e5%b7%a5%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1595","title":{"rendered":"\u5b9c\u9001\u5c0f\u722c\u866b"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u770b\u5173\u4e8e\u722c\u866b\u7684\u4e1c\u897f\uff0c\u6240\u4ee5\u5199\u4e86\u70b9\u7ec3\u7ec3\u624b<br \/>\n\u76ee\u524d\u8fd8\u6ca1\u7ae3\u5de5\uff0c\u53ea\u662f\u5230\u4e86\u80fd\u8fd4\u56deJSON\u6570\u636e\u7684\u9636\u6bb5<br \/>\n\u53c2\u8003\u6587\u732e\u662f\uff1a<a href=\"http:\/\/cuiqingcai.com\/1001.html\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/cuiqingcai.com\/1001.html <\/a>\u00a0\u4f5c\u8005\u5199\u4e86\u4e00\u7cfb\u5217\u7684\u722c\u866b\u6559\u7a0b\uff0c\u5f88\u5b9e\u7528\uff01<br \/>\n\u7528\u5b9c\u9001\u8f6c\u8fd0\u7684\u767b\u5f55\u7ec3\u7ec3\u624b\u5427\uff0c\u53cd\u6b63\u6700\u8fd1\u5165\u4e86\u6df1\u5733\u6d77\u5173\u5751\uff0c\u54ce\uff0c\u3002\u8bf4\u591a\u4e86\u90fd\u662f\u6cea<br \/>\n\u4e3b\u8981\u6ce8\u610f\uff1a<\/p>\n<ol>\n<li>\u5b9c\u9001\u767b\u9646\uff1aHTTP\u5934\u4fe1\u606f\uff0cContent-Type: application\/x-www-form-urlencoded\uff0c\u4e0d\u7136\u76f4\u63a5\u63d0\u793a\u767b\u5f55\u5931\u8d25\u3002\u53ef\u80fd\u56e0\u4e3a\u767b\u5f55\u7684\u4eba\u4e0d\u591a\uff0c\u76ee\u524d\u6ca1\u6709\u8bbe\u7f6e\u9a8c\u8bc1\u7801<\/li>\n<li>\u83b7\u53d6\u8d26\u6237\u8ba2\u5355\u4fe1\u606f\uff1a\u9875\u9762\u52a0\u8f7d\u540e\uff0c\u662fAJAX\u5411\u670d\u52a1\u5668\u4e8c\u6b21\u8bf7\u6c42JSON\u683c\u5f0f\u7684\u8ba2\u5355\u4fe1\u606f\u7684\uff0c\u800c\u4e14\u53d1\u9001\u7684\u8bf7\u6c42\u4e5f\u662fJSON\u683c\u5f0f\u6765POST\u7684\u3002\u8fd4\u56de\u503c\u4e5f\u662fJSON\uff0c\u76f4\u63a5\u514d\u53bb\u4e86\u6b63\u5219\u5339\u914d\u554a\uff01<\/li>\n<\/ol>\n<pre class=\"lang:python decode:true\"># -*- coding: UTF-8 -*-\n__author__ = 'Bowei'\nimport urllib\nimport urllib2\nimport cookielib\nimport json\nclass YiCrawler:\n    statusCode = {\n        'AIRARRDEST': '\u5df2\u5230\u8fbe',\n        'DONE': '\u5df2\u5b8c\u6210',\n        'CLEAR': '\u6e05\u5173\u4e2d',\n        'CONFIRM': '\u5df2\u786e\u8ba4'\n    }\n    def __init__(self):\n        self.userinfo = {'j_username': '',\n                         'j_password': ''}\n        self.cookie = cookielib.CookieJar()\n        self.headers = {'Content-Type': 'application\/x-www-form-urlencoded',\n                        'Cache-Control': 'no-cache'}\n        self.cookie_handler = urllib2.HTTPCookieProcessor(self.cookie)\n        self.opener = urllib2.build_opener(self.cookie_handler)\n        self.results = []   # \u5b58\u653e\u67e5\u8be2\u7ed3\u679c\uff0c\u961f\u5217\u4e2d\u7684\u5143\u7d20\u662f\u5b57\u5178\n    def get_status_str(self, status):\n        if self.statusCode.has_key(status):\n            return self.statusCode[status]\n        else:\n            return status\n    def set_user_info(self, username, password):\n        self.userinfo['j_username'] = username\n        self.userinfo['j_password'] = password\n    def login(self):\n        print 'Login start'\n        login_data = urllib.urlencode(self.userinfo)\n        url = 'https:\/\/www.yi-express.com\/login\/send'\n        request = urllib2.Request(url, login_data, self.headers)\n        try:\n            response = self.opener.open(request)\n            print 'Finished login'\n            return response.getcode()\n        except urllib2.HTTPError, e:\n            print e.code\n            return e.code\n    def get_page(self, pageNo=1, orderType=\"\", warehouse=\"\"):\n        url = 'https:\/\/www.yi-express.com\/customer\/orderSearch'\n        order_params = {\"pageNo\": str(pageNo), \"orderNoOrName\": \"\",\n                        \"warehouse\": warehouse, \"orderType\": orderType,\n                        \"orderCreateStart\": \"\", \"orderCreateEnd\": \"\"}\n        request = urllib2.Request(url)\n        request.add_header('Content-Type', 'application\/json')\n        try:\n            response = self.opener.open(request, json.dumps(order_params))\n            response_decoded = json.loads(response.read())\n            for key, value in response_decoded.iteritems():\n                # print str(key) + ': ' + str(value)\n                if str(key) == 'result':\n                    self.results.extend(value)\n            return response_decoded['hasNext']\n        except urllib2.HTTPError, e:\n            print e.code\n            return False\n    def fetch_all(self):\n        # \u83b7\u53d6\u6240\u6709\u9875\u9762\u7684\u6570\u636e\n        self.results = []\n        hasNext = True\n        pageCount = 1\n        while hasNext:\n            hasNext = self.get_page(pageCount)\n            pageCount += 1\n    def print_results(self):\n        print '========= START ==========='\n        for item in self.results:\n            print 'ID:\\t' + item['id']\n            print 'TIME:\\t' + item['inDateWithFormat']\n            print 'PRICE:\\t' + str(item['realCost'])\n            print 'WEIGHT:\\t' + str(item['realWeight'])\n            print 'EXP_NAME:\\t' + item['expressName']\n            print 'STATUS:\\t' + self.get_status_str(item['statusCode'])\n            print '\\n'\nif __name__ == '__main__':\n    crawler = YiCrawler()\n    print crawler.login()\n    #crawler.get_page(pageNo=1)\n    crawler.fetch_all()\n    crawler.print_results()\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u770b\u5173\u4e8e\u722c\u866b\u7684\u4e1c\u897f\uff0c\u6240\u4ee5\u5199\u4e86\u70b9\u7ec3\u7ec3\u624b \u76ee\u524d\u8fd8\u6ca1\u7ae3\u5de5\uff0c\u53ea\u662f\u5230\u4e86\u80fd\u8fd4\u56deJSON\u6570\u636e\u7684\u9636\u6bb5 \u53c2\u8003\u6587\u732e\u662f\uff1ahttp:\/\/cuiqingcai.com\/1001.html \u00a0\u4f5c\u8005\u5199\u4e86\u4e00\u7cfb\u5217\u7684\u722c\u866b\u6559\u7a0b\uff0c\u5f88\u5b9e\u7528\uff01 \u7528\u5b9c\u9001\u8f6c\u8fd0\u7684\u767b\u5f55\u7ec3\u7ec3\u624b\u5427\uff0c\u53cd\u6b63\u6700\u8fd1\u5165\u4e86\u6df1\u5733\u6d77\u5173\u5751\uff0c\u54ce\uff0c\u3002\u8bf4\u591a\u4e86\u90fd\u662f\u6cea \u4e3b\u8981\u6ce8\u610f\uff1a \u5b9c\u9001\u767b\u9646\uff1aHTTP\u5934\u4fe1\u606f\uff0cContent-Type: application\/x-www-form-urlencoded\uff0c\u4e0d\u7136\u76f4\u63a5\u63d0\u793a\u767b\u5f55\u5931\u8d25\u3002\u53ef\u80fd\u56e0\u4e3a\u767b\u5f55\u7684\u4eba\u4e0d\u591a\uff0c\u76ee\u524d\u6ca1\u6709\u8bbe\u7f6e\u9a8c\u8bc1\u7801 \u83b7\u53d6\u8d26\u6237\u8ba2\u5355\u4fe1\u606f\uff1a\u9875\u9762\u52a0\u8f7d\u540e\uff0c\u662fAJAX\u5411\u670d\u52a1\u5668\u4e8c\u6b21\u8bf7\u6c42JSON\u683c\u5f0f\u7684\u8ba2\u5355\u4fe1\u606f\u7684\uff0c\u800c\u4e14\u53d1\u9001\u7684\u8bf7\u6c42\u4e5f\u662fJSON\u683c\u5f0f\u6765POST\u7684\u3002\u8fd4\u56de\u503c\u4e5f\u662fJSON\uff0c\u76f4\u63a5\u514d\u53bb\u4e86\u6b63\u5219\u5339\u914d\u554a\uff01 # -*- coding: UTF-8 -*- __author__ = &#8216;Bowei&#8217; import urllib import urllib2 import cookielib import json class YiCrawler: statusCode = { &#8216;AIRARRDEST&#8217;: &#8216;\u5df2\u5230\u8fbe&#8217;, &#8216;DONE&#8217;: &#8216;\u5df2\u5b8c\u6210&#8217;, &#8216;CLEAR&#8217;: &#8216;\u6e05\u5173\u4e2d&#8217;, &#8216;CONFIRM&#8217;: &#8216;\u5df2\u786e\u8ba4&#8217; } def __init__(self): self.userinfo = {&#8216;j_username&#8217;: &#8221;, &#8216;j_password&#8217;: &#8221;} self.cookie = cookielib.CookieJar() self.headers = {&#8216;Content-Type&#8217;: &#8216;application\/x-www-form-urlencoded&#8217;, &#8216;Cache-Control&#8217;: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[179],"class_list":["post-1595","post","type-post","status-publish","format-standard","hentry","category-study","tag-179"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1595"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1595\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}