{"id":82166,"date":"2018-09-27T12:44:23","date_gmt":"2018-09-27T04:44:23","guid":{"rendered":"http:\/\/www.fagao.me\/?p=82166"},"modified":"2018-09-27T12:44:23","modified_gmt":"2018-09-27T04:44:23","slug":"python%e7%88%ac%e8%99%ab%e5%ae%9e%e6%88%98%e7%b3%bb%e5%88%974%e7%88%ac%e5%8f%96%e7%99%be%e5%ba%a6%e6%96%b0%e9%97%bb%e5%8f%91%e7%a8%bf%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/www.fagao.me\/p\/82166.htm","title":{"rendered":"Python\u722c\u866b\u5b9e\u6218\u7cfb\u52174\u722c\u53d6\u767e\u5ea6\u65b0\u95fb\u53d1\u7a3f\u4fe1\u606f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.fagao.me\/images\/uploads\/2018_008.jpg\" alt=\"\"><\/p>\n<p><span class=\"bjh-p\"><span class=\"bjh-strong\">\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\u3010\u54c8\u5e0c\u5927\u6570\u636e\u3011<\/span><\/span><\/p>\n<p><span class=\"bjh-p\">\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u4e86scrapy\u7684\u5b89\u88c5\u3001\u5165\u95e8\u6559\u7a0b\uff0c\u4ee5\u53caMongoDB\u7684\u5b89\u88c5\u4e0e\u914d\u7f6e\uff0c\u672c\u7bc7\u5c06\u5206\u4eab\u5982\u4f55\u5229\u7528scrapy\u722c\u53d6\u767e\u5ea6\u65b0\u95fb\u4fe1\u606f\uff0c\u5e76\u5c06\u722c\u53d6\u5230\u7684\u6570\u636e\u5b58\u50a8\u5230MongoDB\u6570\u636e\u5e93\u4e2d\u3002<\/span><\/p>\n<p><span class=\"bjh-h3\">\u6293\u53d6\u76ee\u6807<\/span><\/p>\n<p><span class=\"bjh-p\">\u901a\u8fc7\u7ed9\u5b9a\u5173\u952e\u5b57\uff0c\u722c\u53d6\u767e\u5ea6\u65b0\u95fb\u4e2d\u641c\u7d22\u5230\u7684\u6240\u6709\u6709\u5173\u65b0\u95fb\u4fe1\u606f\uff0c\u65b0\u95fb\u6807\u9898\u3001\u65b0\u95fb\u94fe\u63a5\u3001\u65b0\u95fb\u6765\u6e90\u3002<\/span><\/p>\n<p><span class=\"bjh-h3\">\u6280\u672f\u8def\u7ebf<\/span><\/p>\n<p><span class=\"bjh-p\">\u5229\u7528scrapy\u6846\u67b6\u5b9e\u73b0\u6b64\u6b21\u722c\u53d6\uff0c\u5229\u7528parse\u51fd\u6570\u83b7\u5f97\u9700\u8981\u6293\u53d6\u7684\u4fe1\u606f\uff0c\u901a\u8fc7items\u548cpiplinepipelines\u7684\u8bbe\u7f6e\u5b9e\u73b0\u6570\u636e\u5b58\u50a8\u5230MongoDB\u6570\u636e\u5e93\u4e2d\uff0c\u901a\u8fc7middleware\u8bbe\u7f6euser_agent\u964d\u4f4e\u7f51\u7ad9\u5bf9\u722c\u866b\u7684\u9650\u5236\uff0c\u4f7f\u5f97\u722c\u53d6\u901f\u5ea6\u53ef\u5728\u4e00\u4e2a\u8f83\u9ad8\u7684\u6c34\u5e73\u4e0a\u3002<\/span><\/p>\n<p><span class=\"bjh-h3\">\u76ee\u6807\u7ad9\u70b9\u5206\u6790<\/span><\/p>\n<p><span class=\"bjh-p\">Python\u722c\u866b\u5b9e\u6218\u7cfb\u5217\uff083\uff09\u4e2d\u5df2\u7ecf\u4ecb\u7ecd\u4e86\u5982\u4f55\u5229\u7528chrome\u6d4f\u89c8\u5668\u83b7\u53d6\u5bf9\u5e94\u6807\u7b7e\u7684XPATH\u6216CSS\u8def\u5f84\uff0c\u672c\u6b21\u83b7\u53d6\u65b0\u95fb\u4fe1\u606f\uff0c\u91c7\u7528\u7684\u89e3\u6790\u65b9\u5f0f\u662fCSS\u3002<\/span><\/p>\n<p><span class=\"bjh-h3\">\u7a0b\u5e8f\u7684\u7ed3\u6784\u8bbe\u8ba1<\/span><\/p>\n<p><span class=\"bjh-p\">\u6b65\u9aa41\uff1a\u6253\u5f00\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u8fdb\u5165\u60f3\u65b0\u5efa\u9879\u76ee\u7684\u76ee\u5f55<\/span><\/p>\n<p><span class=\"bjh-p\">\u6b65\u9aa42\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\uff1ascrapy startproject xxxx(\u9879\u76ee\u540d\u79f0) \u65b0\u5efa\u4e00\u4e2a\u9879\u76ee<\/span><\/p>\n<p><span class=\"bjh-p\">\u6b65\u9aa43\uff1a\u8fdb\u5165\u9879\u76ee\u6587\u4ef6\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\uff1ascrapy genspider &lt;\u722c\u866b\u540d\u79f0&gt; &lt;\u722c\u53d6\u7f51\u5740&gt;\u65b0\u5efa\u722c\u866b<\/span><\/p>\n<p><span class=\"bjh-p\">\u6b65\u9aa43\uff1a\u66f4\u6539items.py\u6587\u4ef6\uff0c\u8bbe\u7f6eitem<\/span><\/p>\n<p><span class=\"bjh-p\">\u6b65\u9aa44\uff1a\u7f16\u5199\u722c\u53d6\u4ee3\u7801<\/span><\/p>\n<p><span class=\"bjh-p\">\u6b65\u9aa45\uff1a\u66f4\u6539pipelines\u548cmiddlewares<\/span><\/p>\n<p><span class=\"bjh-p\">\u6b65\u9aa46\uff1a\u542f\u52a8\u722c\u866b\uff0c\u6253\u5f00\u547d\u4ee4\u884c\u8fdb\u5165\u5230\u9879\u76ee\u76ee\u5f55\u4e0b\uff0c\u8f93\u5165\uff1ascrapy crawl &lt;\u722c\u866b\u540d\u79f0&gt;<\/span><\/p>\n<p><span class=\"bjh-p\"><span class=\"bjh-strong\">items.py\u4ee3\u7801\u5982\u4e0b\uff1a<\/span><\/span><\/p>\n<p><span class=\"bjh-p\"># -*- coding: utf-8 -*-<span class=\"bjh-br\"><\/span># Define here the models for your scraped items<span class=\"bjh-br\"><\/span>#<span class=\"bjh-br\"><\/span># See documentation in:<span class=\"bjh-br\"><\/span># http:\/\/doc.scrapy.org\/en\/latest\/topics\/items.html<span class=\"bjh-br\"><\/span>from scrapy import Item, Field<span class=\"bjh-br\"><\/span>class mmwzItem(Item):<span class=\"bjh-br\"><\/span># define the fields for your item here like:<span class=\"bjh-br\"><\/span> # name = Field()<span class=\"bjh-br\"><\/span> article_title = Field()<span class=\"bjh-br\"><\/span> article_url = Field()<span class=\"bjh-br\"><\/span> article_catchroad = Field()<span class=\"bjh-br\"><\/span> article_source = Field()<span class=\"bjh-br\"><\/span><\/span><\/p>\n<p><span class=\"bjh-p\"><span class=\"bjh-strong\">\u672c\u4f8b\u5b50\u7684\u722c\u866b\u540d\u79f0\u4e3a\uff1axinwen\uff0c\u722c\u866b\u6587\u4ef6xinwen.py\u7684\u4ee3\u7801\u4e3a\uff1a<\/span><\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.fagao.me\/images\/uploads\/2017_004.jpg\" alt=\"\"><\/p>\n<p><span class=\"bjh-p\"># -*- coding: utf-8 -*-<span class=\"bjh-br\"><\/span>from urllib.parse import urlencode<span class=\"bjh-br\"><\/span>import re<span class=\"bjh-br\"><\/span>from scrapy import Spider, Request<span class=\"bjh-br\"><\/span>from scrapy_mmwz.items import mmwzItem<span class=\"bjh-br\"><\/span>class XinwenSpider(Spider):<span class=\"bjh-br\"><\/span>name = &#8220;xinwen&#8221;<span class=\"bjh-br\"><\/span> keyword = &#8216;\u6bd4\u7279\u5e01&#8217;<span class=\"bjh-br\"><\/span> page = 0<span class=\"bjh-br\"><\/span> #\u67e5\u8a62\u4fe1\u606f<span class=\"bjh-br\"><\/span> data = {<span class=\"bjh-br\"><\/span> &#8216;word&#8217;: keyword,<span class=\"bjh-br\"><\/span> &#8216;pn&#8217;: page,<span class=\"bjh-br\"><\/span> &#8216;cl&#8217;: &#8216;2&#8217;,<span class=\"bjh-br\"><\/span> &#8216;ct&#8217;: &#8216;1&#8217;,<span class=\"bjh-br\"><\/span> &#8216;tn&#8217;: &#8216;news&#8217;,<span class=\"bjh-br\"><\/span> &#8216;rn&#8217;: &#8217;20&#8217;,<span class=\"bjh-br\"><\/span> &#8216;ie&#8217;: &#8216;utf &#8211; 8&#8217;,<span class=\"bjh-br\"><\/span> }<span class=\"bjh-br\"><\/span> # \u751f\u6210URL\u7684\u53c2\u6570\u90e8\u5206<span class=\"bjh-br\"><\/span> params = urlencode(data)<span class=\"bjh-br\"><\/span> base = &#8216;http:\/\/news.baidu.com\/ns?&#8217;<span class=\"bjh-br\"><\/span> url = base + params<span class=\"bjh-br\"><\/span> allowed_domains = [&#8220;news.baidu.com&#8221;]<span class=\"bjh-br\"><\/span> start_urls = [url]<span class=\"bjh-br\"><\/span> def parse(self, response):<span class=\"bjh-br\"><\/span> item = mmwzItem()<span class=\"bjh-br\"><\/span> if response.status == 200:<span class=\"bjh-br\"><\/span> news_lists = response.css(&#8216;#wrapper_wrapper #content_left div.result&#8217;)<span class=\"bjh-br\"><\/span> page_number = response.css(&#8216;p#page strong span.pc::text&#8217;).extract_first()<span class=\"bjh-br\"><\/span> # print(news_lists)<span class=\"bjh-br\"><\/span> print(&#8216;page_number:&#8217;, page_number)<span class=\"bjh-br\"><\/span> if news_lists:<span class=\"bjh-br\"><\/span> for news in news_lists:<span class=\"bjh-br\"><\/span>  # news_lists\u662f\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u5728\u8c03\u7528\u51fd\u6570\u662f\u53ef\u4ee5\u7528for\u5faa\u73af\u4f9d\u6b21\u83b7\u53d6\u7ed3\u679c<span class=\"bjh-br\"><\/span>  lists = {<span class=\"bjh-br\"><\/span>  &#8216;article_url&#8217;: news.css(&#8216;.c-title a::attr(href)&#8217;).extract_first(),<span class=\"bjh-br\"><\/span>  &#8216;article_title&#8217;: news.css(&#8216;.c-title a::text&#8217;).extract_first(),<span class=\"bjh-br\"><\/span>  &#8216;article_catchroad&#8217;: &#8216;baidu&#8217;,<span class=\"bjh-br\"><\/span>  &#8216;article_source&#8217;: re.search(re.compile(&#8216;(.*?)\\\\xa0&#8217;, re.S),<span class=\"bjh-br\"><\/span>    news.css(&#8216;p.c-author::text&#8217;).extract_first()).group(1)<span class=\"bjh-br\"><\/span>  }<span class=\"bjh-br\"><\/span>  for field in item.fields:<span class=\"bjh-br\"><\/span>  if field in lists.keys():<span class=\"bjh-br\"><\/span>  item[field] = lists.get(field)<span class=\"bjh-br\"><\/span>  # \u5b83\u76f8\u5f53\u4e8ereturn\u53ea\u4e0d\u8fc7\u4e00\u6b21\u8fd4\u56de\u4e00\u4e2a<span class=\"bjh-br\"><\/span>  yield item<span class=\"bjh-br\"><\/span> print(response.css(&#8216;p#page a:last-child::text&#8217;).extract_first())<span class=\"bjh-br\"><\/span> if response.css(&#8216;p#page a:last-child::text&#8217;).extract_first() == &#8216;\u4e0b\u4e00\u9875&gt;&#8217;:<span class=\"bjh-br\"><\/span> # \u7372\u53d6\u4e0b\u4e00\u9801\u93c8\u63a5<span class=\"bjh-br\"><\/span> next_page = &#8216;http:\/\/news.baidu.com&#8217; + response.css(&#8216;p#page a:last-child::attr(href)&#8217;).extract_first()<span class=\"bjh-br\"><\/span> # \u5be6\u73fe\u5faa\u74b0\u7372\u53d6\u4e0b\u4e00\u9801\u5167\u5bb9<span class=\"bjh-br\"><\/span> yield Request(next_page, callback=self.parse)<span class=\"bjh-br\"><\/span><\/span><\/p>\n<p><span class=\"bjh-p\"><span class=\"bjh-strong\">\u901a\u8fc7pipelines\u8bbe\u7f6e\uff0c\u5c06\u722c\u53d6\u5230\u7684\u6570\u636e\u6210\u529f\u4fdd\u5b58\u5230MongoDB\u6570\u636e\u5e93\u4e2d\uff0c\u8bbe\u7f6e\u8be6\u7ec6\u4ee3\u7801\u5982\u4e0b\uff1a<\/span><\/span><\/p>\n<p><span class=\"bjh-p\"># -*- coding: utf-8 -*-<span class=\"bjh-br\"><\/span># Define your item pipelines here<span class=\"bjh-br\"><\/span>#<span class=\"bjh-br\"><\/span># Don&#8217;t forget to add your pipeline to the ITEM_PIPELINES setting<span class=\"bjh-br\"><\/span># See: http:\/\/doc.scrapy.org\/en\/latest\/topics\/item-pipeline.html<span class=\"bjh-br\"><\/span>import pymongo<span class=\"bjh-br\"><\/span>class MongoPipeline(object):<span class=\"bjh-br\"><\/span>collection_name = &#8216;xinwen&#8217;<span class=\"bjh-br\"><\/span> def __init__(self, mongo_uri, mongo_db):<span class=\"bjh-br\"><\/span>  self.mongo_uri = mongo_uri<span class=\"bjh-br\"><\/span>  self.mongo_db = mongo_db<span class=\"bjh-br\"><\/span> @classmethod<span class=\"bjh-br\"><\/span> def from_crawler(cls, crawler):<span class=\"bjh-br\"><\/span>  return cls(<span class=\"bjh-br\"><\/span>   mongo_uri=crawler.settings.get(&#8216;MONGO_URI&#8217;),<span class=\"bjh-br\"><\/span>   mongo_db=crawler.settings.get(&#8216;MONGO_DATABASE&#8217;, &#8216;items&#8217;)<span class=\"bjh-br\"><\/span>  )<span class=\"bjh-br\"><\/span> def open_spider(self, spider):<span class=\"bjh-br\"><\/span>  self.client = pymongo.MongoClient(self.mongo_uri)<span class=\"bjh-br\"><\/span>  self.db = self.client[self.mongo_db]<span class=\"bjh-br\"><\/span> def close_spider(self, spider):<span class=\"bjh-br\"><\/span>  self.client.close()<span class=\"bjh-br\"><\/span> # def process_item(self, item, spider):<span class=\"bjh-br\"><\/span> #  self.db[&#8216;user&#8217;].update({&#8216;url_token&#8217;: item[&#8216;url_token&#8217;]}, {&#8216;$set&#8217;: item}, True)<span class=\"bjh-br\"><\/span> #<span class=\"bjh-br\"><\/span> #  return item<span class=\"bjh-br\"><\/span> def process_item(self, item, spider):<span class=\"bjh-br\"><\/span>  self.db[self.collection_name].insert(dict(item))<span class=\"bjh-br\"><\/span>  return item<span class=\"bjh-br\"><\/span><\/span><\/p>\n<p><span class=\"bjh-p\"><span class=\"bjh-strong\">\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u4e24\u70b9\uff0c\u7b2c\u4e00\u9996\u5148\u5b89\u88c5pymongo\u5305\uff0c\u7b2c\u4e8c\u5728settings.py\u6587\u4ef6\u505a\u5982\u4e0b\u8bbe\u7f6e\uff1a<\/span><\/span><\/p>\n<p><span class=\"bjh-p\">MONGO_URI = &#8216;localhost&#8217;<span class=\"bjh-br\"><\/span>MONGO_DATABASE = &#8216;xinwen&#8217;<\/span><\/p>\n<p><span class=\"bjh-p\">settings.py\u6587\u4ef6\u4e2d\u8fd8\u6709\u4e00\u4e2a\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u662f\uff1a<\/span><\/p>\n<p><span class=\"bjh-p\"># Obey robots.txt rules<span class=\"bjh-br\"><\/span>ROBOTSTXT_OBEY = False<\/span><\/p>\n<p><span class=\"bjh-p\">\u975e\u5e38\u591a\u7684\u7f51\u7ad9\u90fd\u6709robots\u534f\u8bae\uff0c\u4e0d\u5141\u8bb8\u722c\u53d6\u672c\u7f51\u7ad9\u4fe1\u606f\uff0c\u5982\u679c\u8fd9\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e3aTrue\uff0c\u975e\u5e38\u591a\u7684\u7f51\u7ad9\u90fd\u4e0d\u80fd\u722c\u53d6\u4e86\u3002<\/span><\/p>\n<p><span class=\"bjh-p\"><span class=\"bjh-strong\">\u5b8c\u6574\u9879\u76ee\u4ee3\u7801\u8bf7\u53c2\u8003\u6211\u7684GitHub\uff1a<\/span><span class=\"bjh-strong\">https:\/\/github.com\/kxylxx\/scrapy_testproject<\/span><\/span><\/p>\n<p><span class=\"bjh-p\">\u4e3a\u4e86\u65b9\u4fbf\u67e5\u770b\u722c\u53d6\u5230\u7684\u4fe1\u606f\uff0c\u8fd9\u91cc\u7ed9\u5927\u5bb6\u63a8\u8350\u4e00\u4e2aMongoDB\u53ef\u89c6\u5316\u8f6f\u4ef6Robo 3T,\u8be6\u7ec6\u5b89\u88c5\u4f7f\u7528\u6559\u7a0b\u8bf7\u53c2\u8003\uff1ahttps:\/\/www.cnblogs.com\/dacongge\/p\/7346037.html \uff0c\u611f\u8c22\u5927\u8471\u54e5\u7684\u5206\u4eab\u3002<\/span><\/p>\n<p><span class=\"bjh-p\"><span class=\"bjh-strong\">\u7ed3\u679c\u5c55\u793a\u53ca\u5c0f\u7ed3<\/span><\/span><\/p>\n<p><span class=\"bjh-p\"><span class=\"bjh-strong\">\u83b7\u53d6\u5230\u7684\u6570\u636e\u5982\u4e0b\u56fe\u6240\u793a<\/span>\uff1a<\/span><\/p>\n<div class=\"img-container\"><\/div>\n<p><span class=\"bjh-h3\">\u5c0f\u7ed3\uff1a<\/span><\/p>\n<p><span class=\"bjh-p\">\u901a\u8fc7\u672c\u6b21\u5b9e\u6218\u5e0c\u671b\u5927\u5bb6\u5bf9scrapy\u6846\u67b6\u6709\u66f4\u6df1\u523b\u7684\u8ba4\u8bc6\uff0c\u5e0c\u671b\u80fd\u591f\u6839\u636e\u8be5\u6848\u4f8b\u8bbe\u8ba1\u4e00\u4e2a\u57fa\u4e8escrapy\u6846\u67b6\u7684\u722c\u866b\u7a0b\u5e8f\uff0c\u4ee5\u719f\u6089\u6574\u4e2a\u722c\u53d6\u6d41\u7a0b\u3002\u5e0c\u671b\u5927\u5bb6\u80fd\u591f\u81ea\u5df1\u6572\u5199\u4ee3\u7801\uff0c\u5728\u6572\u4ee3\u7801\u7684\u8fc7\u7a0b\u4e2d\u53d1\u73b0\u95ee\u9898\u89e3\u51b3\u95ee\u9898\u3002<\/span><\/p>\n<div class=\"img-container\"><\/div>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\u3010\u54c8\u5e0c\u5927\u6570\u636e\u3011 \u524d\u9762\u6211\u4eec\u5df2\u7ecf\u4ecb\u7ecd\u4e86scrapy\u7684\u5b89\u88c5\u3001\u5165\u95e8\u6559\u7a0b\uff0c\u4ee5\u53caMongoDB\u7684\u5b89\u88c5\u4e0e\u914d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[120],"class_list":["post-82166","post","type-post","status-publish","format-standard","hentry","category-news","tag-120"],"_links":{"self":[{"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/posts\/82166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/comments?post=82166"}],"version-history":[{"count":0,"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/posts\/82166\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/media?parent=82166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/categories?post=82166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fagao.me\/p\/wp-json\/wp\/v2\/tags?post=82166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}