{"id":34,"date":"2019-11-04T17:23:55","date_gmt":"2019-11-04T09:23:55","guid":{"rendered":"http:\/\/47.110.70.105\/wordpress\/?p=34"},"modified":"2019-11-04T17:29:15","modified_gmt":"2019-11-04T09:29:15","slug":"%e5%9f%ba%e4%ba%8epyspider%e7%9a%84weibo-cn%e7%88%ac%e8%99%ab","status":"publish","type":"post","link":"http:\/\/43.139.139.10\/wordpress\/34\/","title":{"rendered":"\u57fa\u4e8ePyspider\u7684weibo.cn\u722c\u866b"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"\u4e00\u4e9b\u5b66\u4e60\u8d44\u6599\">\u4e00\u4e9b\u5b66\u4e60\u8d44\u6599<\/h1>\n\n\n\n<ol><li><a href=\"https:\/\/link.jianshu.com\/?t=http:\/\/xijiacs.lofter.com\/post\/1d96cd64_a0f4013\">Fiddler\u7b80\u6613\u4f7f\u7528\u6559\u7a0b \u6293cookies\u7528<strong>\uff08\u5fc5\u770b\uff09<\/strong><\/a><\/li><li><a href=\"https:\/\/www.jianshu.com\/p\/36290e6acf45\">PySpider\u7b80\u6613\u6559\u7a0b \u6574\u4e2a\u722c\u866b\u7528\u5230\u7684\u6846\u67b6<strong>\uff08\u5fc5\u770b\uff09<\/strong><\/a><\/li><li><a href=\"https:\/\/link.jianshu.com\/?t=http:\/\/my.oschina.net\/abian\/blog\/131548?fromerr=rY5qyfx5\">HTTP Header\u5165\u95e8\u8be6\u89e3<\/a>&nbsp;\u5728\u6a21\u62df\u767b\u5f55\u8fc7\u7a0b\u4e2d\u8981\u6d89\u53ca\u5230http\u5934\u7684\u8bbe\u7f6e\uff0c\u9700\u8981\u4e86\u89e3\u57fa\u672c\u4fe1\u606f<\/li><li><a href=\"https:\/\/link.jianshu.com\/?t=http:\/\/blog.csdn.net\/u010487568\/article\/details\/46932839\">\u5168\u7a0b\u6a21\u62df\u65b0\u6d6a\u5fae\u535a\u767b\u5f552015<\/a>&nbsp;\u8fd9\u4e2a\u5206\u6790\u4e86\u65b0\u6d6a\u5fae\u535a\u7f51\u9875\u7248\u73b0\u5728\u8fd8\u5728\u7528\u7684\u767b\u5f55\u8fc7\u7a0b\uff0c\u5176\u4e2d\u4f7f\u7528\u5230\u7684\u6700\u65b0\u811a\u672cssologin.js\u7248\u672c\u53f71.4.18\u3002\u4e0d\u8fc7\u672c\u6587\u4e3b\u8981\u662f\u57fa\u4e8ewap\u7248\u7684\u722c\u866b\uff0c\u8fd9\u4e2a\u6ca1\u5fc5\u8981\u770b\uff0c\u5982\u679c\u60f3\u8fdb\u4e00\u6b65\u722c\u53d6\u7f51\u9875\u7248\u7684\u5fae\u535a\u53ef\u4ee5\u53c2\u8003\u5b66\u4e60\u3002<\/li><li><a href=\"https:\/\/link.jianshu.com\/?t=http:\/\/blog.csdn.net\/dipolar\/article\/details\/49661083\">Sina\u5fae\u535a\u722c\u53d6@pyspider<\/a>&nbsp;\u8fd9\u7bc7\u6587\u7ae0\u901a\u8fc7\u7528\u6237\u540d\u548c\u5bc6\u7801\u83b7\u53d6wap\u7248\u5fae\u535a\u7684cookies\u540e\u518d\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\uff0c\u4e5f\u53ef\u53c2\u8003\u3002<\/li><li><a href=\"https:\/\/link.jianshu.com\/?t=https:\/\/pythonhosted.org\/pyquery\/index.html\">PyQuery\u6587\u6863<\/a>&nbsp;<code>&amp;&amp;<\/code>&nbsp;<a href=\"https:\/\/link.jianshu.com\/?t=http:\/\/www.w3schools.com\/cssref\/css_selectors.asp\">CSS\u9009\u62e9\u5668<\/a><code>&amp;&amp;<\/code><a href=\"https:\/\/link.jianshu.com\/?t=https:\/\/docs.python.org\/2\/library\/re.html\">Python\u6b63\u5219\u8868\u8fbe\u5f0fre package\u6587\u6863<\/a>&nbsp;<code>Python<\/code>\u9875\u9762\u722c\u4e0b\u6765\u4ee5\u540e\u7528\u4e8e\u64cd\u4f5c\u9875\u9762\u5143\u7d20\u83b7\u53d6\u9700\u8981\u7684\u5185\u5bb9<strong>\uff08\u5fc5\u770b\uff09<\/strong><\/li><\/ol>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"\u83b7\u53d6cookies\u7528\u4e8e\u6a21\u62df\u767b\u9646\">\u83b7\u53d6cookies\u7528\u4e8e\u6a21\u62df\u767b\u9646<\/h1>\n\n\n\n<p>\u53c2\u8003\u5b66\u4e60\u8d44\u65991\u91cc\u9762\u8bbe\u7f6e\u597dfiddler\uff0c\u7136\u540e\u6253\u5f00<a>http:\/\/weibo.cn\/<\/a>&nbsp;\u6ca1\u767b\u9646\u7684\u8bdd\u767b\u9646\u3002\u5728\u767b\u5f55\u7684\u72b6\u6001\u4e0b\u6253\u5f00\u4f60\u8981\u8fdb\u884c\u722c\u53d6\u7684\u9875\u9762\uff0c\u89c2\u5bdffiddler\u91cc\u6293\u5230\u7684\u5305\uff0c\u5f97\u5230\u9700\u8981\u7684cookies\u4fe1\u606f<\/p>\n\n\n\n<p>\u83b7\u53d6cookies \u5bf9\u5e94\u8bbe\u7f6e\u597dPySpider\u91cc\u7684<code>crawl_config<\/code>\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f<code>cookies<\/code>\u548c<code>headers<\/code>\uff0c\u8003\u8651\u5230\u6211\u7684\u4ee3\u7801\u903b\u8f91\u6211\u628a\u8fd9\u4e24\u4e2a\u90fd\u653e\u5728<code>crawl_config<\/code>\u91cc\u800c\u4e0d\u662f\u5177\u4f53\u7684\u722c\u53d6\u51fd\u6570\u91cc\u3002\u5173\u4e8e\u5934\u7684\u8bbe\u7f6e\u4e5f\u53ef\u4ee5\u53c2\u8003fiddler\u91cc<code>Headers<\/code>\u4e00\u680f\uff0c\u5b8c\u6574\u4ee3\u7801\u8d34\u5728\u6700\u540e\u53ef\u4ee5\u53c2\u8003\u6211\u7684\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"\u722c\u866b\u903b\u8f91\">\u722c\u866b\u903b\u8f91<\/h1>\n\n\n\n<p>\u6211\u7684\u9700\u6c42\u662f\u5bf9\u4e8e\u6307\u5b9a\u7684\u7528\u6237\uff0c\u7ed9\u51fa\u7528\u6237\u4e3b\u9875\uff08\u5f62\u5982<code>http:\/\/weibo.cn\/kaikai0818<\/code>\u6216<code>http:\/\/weibo.cn\/u\/1788136742<\/code>\uff09\uff0c\u722c\u53d6TA\u5168\u90e8\u7684\u5fae\u535a\u53ca\u76f8\u5173\u4fe1\u606f\uff08\u5982\u53d1\u5e03\u65f6\u95f4\u3001\u8f6c\u53d1\u6570\u7b49\uff09\u3002<\/p>\n\n\n\n<p>\u5165\u53e3\u51fd\u6570<code>on_start<\/code>\u6ca1\u4ec0\u4e48\u53ef\u8bf4\u7684\uff0c\u56e0\u4e3a\u8bbe\u7f6e\u4e86\u5168\u5c40\u7684headers\u548ccookies\u8fd9\u8fb9\u5728<code>self.crawl<\/code>\u7684\u65f6\u5019\u5c31\u4e0d\u9700\u8981\u518d\u53d1\u9001\u4e86\uff0c\u5982\u679c\u6ca1\u8bbe\u7f6e\u6210\u5168\u5c40\u7684\uff0c\u5fc5\u987b\u5728\u8fd9\u8fb9\u8bbe\u7f6e\u5e76\u53d1\u9001\u3002<\/p>\n\n\n\n<p><code>index_page<\/code>\u51fd\u6570\u4e3b\u8981\u7528\u6765\u8ba1\u7b97\u4e00\u5171\u6709\u591a\u5c11\u9875\u9700\u8981\u722c\u53d6\uff0c\u9875\u9762\u6709\u4e00\u4e2a\u7c7b\u578b\u4e3a<code>hidden<\/code>\u7684<code>&lt;input&gt;<\/code>\u5143\u7d20\u8bb0\u5f55\u4e86\u4e00\u5171\u6709\u591a\u5c11\u9875\uff0c\u7136\u540e\u4e00\u4e2afor\u5f97\u5230\u6240\u6709\u9700\u8981\u722c\u53d6\u7684\u9875\u9762\u5730\u5740\u3002<\/p>\n\n\n\n<p><code>list_single_page<\/code>\u7528\u4e8e\u5904\u7406\u6bcf\u4e00\u9875\u5fae\u535a\u7684\u5185\u5bb9\uff0c\u4e00\u9875\u6709\u5341\u6761\uff0c\u4e3b\u8981\u5c31\u662f\u64cd\u4f5c\u9875\u9762\u83b7\u53d6\u5355\u6761\u5fae\u535a\u7684\u5730\u5740\uff08\u56e0\u4e3a\u540e\u7eed\u9700\u6c42\u53ef\u80fd\u9700\u8981\u722c\u53d6\u6bcf\u4e00\u6761\u5fae\u535a\u7684\u8f6c\u53d1\u548c\u8bc4\u8bba\u5185\u5bb9\uff09\u3002\u8fd9\u8fb9\u6709\u4e2atrick\uff0c\u548cweb\u7248\u5fae\u535a\u4e0d\u540c\u7684\u662f\uff0cwap\u7248\u5355\u6761\u5fae\u535a\u5e76\u6ca1\u6709\u4e00\u4e2a\u6240\u8c13\u7684\u5730\u5740\uff0c\u53ea\u6709\u4e00\u4e2a\u8f6c\u53d1\u6216\u8005\u8bc4\u8bba\u7684\u5730\u5740\uff0c\u4ee3\u7801\u91cc\u7528\u4e86\u8f6c\u53d1\u7684\u5730\u5740\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f<strong>\u6211\u81ea\u5df1\u6d4b\u8bd5\u8fd9\u4e2a\u722c\u866b\u7684\u65f6\u5019\u722c\u4e861500\u6761\u5fae\u535a\u5de6\u53f3\u7684\u65f6\u5019\u8d26\u53f7\u88ab\u51bb\u7ed3\u4e86\uff0c\u5efa\u8bae\u5728dashboard\u8c03\u4e00\u4e0brate\u3001burst\u503c\u3002<\/strong>\u4e0d\u8fc7\u540e\u6765\u767b\u5f55\u540e\u6fc0\u6d3b\u4e86\u4e00\u4e0b\u8d26\u53f7\u53c8\u80fd\u7528\u4e86\uff0c\u8fd8\u6ca1\u6d4b\u8bd5\u8fc7\u591a\u5c11\u503c\u53ef\u4ee5\u907f\u514d\u88ab\u51bb\uff0c\u56e0\u4e3a\u4e0d\u6e05\u695a\u65b0\u6d6a\u51bb\u7ed3\u7684\u673a\u5236\uff0c\u6211\u7684\u5c0f\u53f7\u8fd8\u8981\u7528\u6765\u82b1\u75f4\u7684\uff0c\u4e0d\u6562\u778e\u641e\u4e86_(:\u0437\u300d\u2220)_<\/p>\n\n\n\n<p><code>detail_page<\/code>\u7528\u6765\u5904\u7406\u5355\u6761\u5fae\u535a\uff0c\u7136\u540e\u5c31\u662fcss\u9009\u62e9\u5668+\u6b63\u5219\u7684\u5e94\u7528\u4e86\u54c7\uff0c\u6ca1\u4ec0\u4e48\u53ef\u8bf4\u7684\uff0c\u5c31\u662f\u627e\u89c4\u5f8b\uff0c\u76ee\u524d\u6d4b\u8bd5\u8fc7\u4e86\u6ca1\u5565\u95ee\u9898\uff0c\u4e0d\u8fc7\u722c\u4e0b\u6765\u76841500\u591a\u6761\u4e5f\u6ca1\u8ba4\u771f\u68c0\u67e5\u8fc7\uff0c\u5927\u5bb6\u81ea\u5df1\u518d\u770b\u770b\u4e86\u54c7\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2a\u80fd\u8dd1\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n# -*- encoding: utf-8 -*-\n# Created on 2016-03-07 13:26:00\n# Project: user_timeline\n\nimport re\nimport time\nfrom pyquery import PyQuery\nfrom pyspider.libs.base_handler import *\n\nclass Handler(BaseHandler):\n    user_url = \"\"http:\/\/weibo.cn\/kaikai0818\"\"\n\n    crawl_config = {\n        'itag': 'v1',\n\n        'headers': {\n            'User-Agent': 'Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko\/20100101 Firefox\/44.0',\n            \"\"Host\"\": \"\"weibo.cn\"\",\n            \"\"Accept\"\": \"\"text\/html,application\/xhtml+xml,application\/xml;q=0.9,*\/*;q=0.8\"\",\n            \"\"Accept-Language\"\": \"\"zh-CN,zh-TW;q=0.8,zh-HK;q=0.6,en-US;q=0.4,en;q=0.2\"\",\n            \"\"Accept-Encoding\"\": \"\"gzip, deflate\"\",\n            \"\"DNT\"\": \"\"1\"\",\n            \"\"Connection\"\": \"\"keep-alive\"\"\n        },\n\n        'cookies': {\n            \"\"_T_WM\"\":\"\"I\u2764kkw(\u00af\ufe43\u00af)\"\",\n            \"\"SUB\"\":\"\"I\u2764kkw(\u00af\ufe43\u00af)\"\",\n            \"\"gsid_CTandWM\"\":\"\"I\u2764kkw(\u00af\ufe43\u00af)\"\",\n            \"\"_T_WL\"\":\"\"1\"\",\n            \"\"_WEIBO_UID\"\":\"\"I\u2764kkw(\u00af\ufe43\u00af)\"\" \n        }\n    }\n\n    @every(minutes=24 * 60)\n    def on_start(self):        \n        self.crawl(Handler.user_url, callback=self.index_page,method=\"\"GET\"\")   \n\n    @config(age=1 * 24 * 60 * 60)        \n    def index_page(self, response):\n        #\u8ba1\u7b97\u8be5\u7528\u6237\u7684\u5fae\u535a\u5171\u6709\u51e0\u9875\n        pages = response.doc(\"\"[type=hidden]\"\").attr[\"\"value\"\"]            \n        for i in range(1,int(pages)+1):\n            self.crawl(Handler.user_url+\"\"?page=\"\"+str(i), callback=self.list_single_page,method=\"\"GET\"\")\n\n    @config(priority=2) #\u6570\u5b57\u8d8a\u5927\u4f18\u5148\u7ea7\u8d8a\u9ad8\n    def list_single_page(self, response):\n        #\u5904\u7406\u5f53\u524d\u9875\u7684\u5fae\u535a\u00a0\n        url = \"\"http:\/\/weibo.cn\/repost\/\"\"\n        for each in response.doc(\"\"div[id^=\\\"\"M_\\\"\"]\"\").items() : mid = each.attr(\"\"id \"\") self.crawl(url + mid[2 : ], cookies = response.cookies, callback = self.detail_page)#self.crawl\u53ef\u52a0\u4e0a\u53c2\u6570exetime = time.time() + 1 * 60\uff08\u5373\u4e00\u5206\u949f\u540e\u518dcrawl\uff09\n\n@config(priority = 1) def detail_page(self, response) : res = response.doc(\"\"#M_ \"\")\n\n#\u5224\u65ad\u662f\u5426\u4e3a\u8f6c\u53d1\nif len(res.find(\"\"span.cmt \"\")) != 0 : is_rt = 1\nelse: is_rt = 0\n\n#\u5224\u65ad\u662f\u5426\u542b\u56fe\u7247\nif len(res.children(\"\"div \"\").eq(1).find(\"\"img \"\")) != 0 : has_pic = 1\nelse: has_pic = 0\n\nif is_rt == 0 : text = res.find(\"\"span.ctt \"\").text() orig_user = \"\"NA \"\"orig_text = \"\"NA \"\"\nelse: orig_user = res(\"\"div: first - child span.cmt a \"\").text() orig_text = res.find(\"\"span.ctt \"\").text() td = re.findall(r '&lt;\/span>([\\s\\S]+)&lt;span class=', res(\"\"div: last - child \"\").html()) text = PyQuery(td[0]).text() return {\n    \"\"screen_name \"\": res(\"\"div: first - child &amp; gt; a: first - child \"\").text(),\n    \"\"time \"\": res.find(\"\"span.ct \"\").text(),\n    \"\"text \"\": text,\n    \"\"is_rt \"\": is_rt,\n    \"\"has_pic \"\": has_pic,\n    \"\"orig_user \"\": orig_user[1 : ],\n    \"\"orig_text \"\": orig_text,\n    \"\"repo_cnt \"\": re.search('\\d+', response.doc(\"\"span.pms \"\").text()).group(),\n    \"\"cmt_cnt \"\": re.search('\\d+', response.doc(\"\"span.pms \"\").siblings().eq(0).text()).group(),\n    \"\"atti_cnt \"\": re.search('\\d+', response.doc(\"\"span.pms \"\").siblings().eq(1).text()).group()\n} &lt; \/code>\n<\/code><\/pre>\n\n\n\n<p>\u539f\u6587\u4f5c\u8005\uff1a<a href=\"https:\/\/www.jianshu.com\/p\/c15102a6eb21\">\u516e\u5609<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e9b\u5b66\u4e60\u8d44\u6599 Fiddler\u7b80\u6613\u4f7f\u7528\u6559\u7a0b \u6293cookies\u7528\uff08\u5fc5\u770b\uff09PySpider\u7b80\u6613\u6559\u7a0b \u6574\u4e2a\u722c\u866b\u7528\u5230\u7684\u6846&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"http:\/\/43.139.139.10\/wordpress\/34\/\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[7],"tags":[3],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pbrrw3-y","_links":{"self":[{"href":"http:\/\/43.139.139.10\/wordpress\/wp-json\/wp\/v2\/posts\/34"}],"collection":[{"href":"http:\/\/43.139.139.10\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/43.139.139.10\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/43.139.139.10\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/43.139.139.10\/wordpress\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":1,"href":"http:\/\/43.139.139.10\/wordpress\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":35,"href":"http:\/\/43.139.139.10\/wordpress\/wp-json\/wp\/v2\/posts\/34\/revisions\/35"}],"wp:attachment":[{"href":"http:\/\/43.139.139.10\/wordpress\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/43.139.139.10\/wordpress\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/43.139.139.10\/wordpress\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}