{"id":102517,"date":"2020-05-03T19:17:39","date_gmt":"2020-05-03T11:17:39","guid":{"rendered":"http:\/\/4563.org\/?p=102517"},"modified":"2020-05-03T19:17:39","modified_gmt":"2020-05-03T11:17:39","slug":"%e8%af%b7%e6%95%99%e4%b8%8b%ef%bc%8c-python-%e9%87%8c%e6%8f%92%e5%85%a5-mongo-%e4%b8%80%e7%9b%b4%e6%8a%a5%e9%94%99","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=102517","title":{"rendered":"\u8bf7\u6559\u4e0b\uff0c Python \u91cc\u63d2\u5165 mongo \u4e00\u76f4\u62a5\u9519"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e0b\uff0c Python \u91cc\u63d2\u5165 mongo \u4e00\u76f4\u62a5\u9519               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Colorful <\/span>  <span><i><\/i> 33<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        import pymongo<br \/>from pymongo import MongoClient<br \/>client = pymongo.MongoClient(host=&#8217;127.0.0.1&#8242;, port=27017)<br \/>collection = client.newtext<br \/>print(&#8216;============&#8217;,collection )<\/p>\n<p>class FirstPipeline(object):<\/p>\n<p> def process_item(self, item, spider):<br \/> for i in range(0,len(item[&#8220;content&#8221;])):<br \/> arrtext = {&#8216;title&#8217;:item[&#8216;title&#8217;][i],&#8217;price&#8217;:item[&#8220;price&#8221;][i],&#8217;content&#8217;:item[&#8220;content&#8221;][i]}<br \/> collection.insert_many(arrtext)<br \/> return item<\/p>\n<p>\u8fd9\u662f scapy \u6846\u67b6\uff0c\u5728 pipelines.py \u9875\u9762\u91cc\u9762\u5199\u7684\uff0c\u4e0a\u9762\u662f\u8fd9\u4e2a\u9875\u9762\u6240\u6709\u7684\u4ee3\u7801 <\/p>\n<p>\u6253\u5370\u51fa\u6765\u7684\u503c\u662f\u8fd9\u6837\u7684<\/p>\n<p>Database(MongoClient(host=[&#8216;127.0.0.1:27017&#8217;], document_class=dict, tz_aware=False, connect=True), &#8216;newtext&#8217;)<\/p>\n<p>\u83dc\u9e1f\u4e00\u679a      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"682148\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Nitroethane <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             insert_many \u7684\u5f97\u662f\u6570\u7ec4\u5427\uff0c\u4fdd\u5b58\u5b57\u5178\u5e94\u8be5\u7528 insert_one \uff08\u5fd8\u8bb0\u4e86\u662f insert \u8fd8\u662f insert_one\uff0c\u770b\u4e0b\u6587\u6863\u5c31\u77e5\u9053\u4e86\uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"682149\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Colorful <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Nitroethane insert \u4e5f\u4e0d\u884c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"682150\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : caly5144 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u540c\u83dc\u9e1f\uff0c\u4e0d\u8fc7\u6ca1\u6709\u89c1\u8fc7 client.newtext \u8fd9\u6837\u7684\u8bed\u6cd5\u3002\u6211\u5199\u7684\u65f6\u5019\u4e00\u822c\u662f<br \/>myclient = pymongo.MongoClient(&#8220;mongodb:\/\/localhost:27017\/&#8221;) <br \/>mydb = myclient[&#8220;database&#8221;] # \u521b\u5efa \/\u8fde\u63a5\u6570\u636e\u5e93<br \/>mycol = mydb[&#8220;collection&#8221;] # \u521b\u5efa \/\u8fde\u63a5\u96c6\u5408<\/p>\n<p>mycol.insert_one({}) # \u63d2\u5165\u4e00\u6761\u6570\u636e                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"682151\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JCZ2MkKb5S8ZX9pq <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Nitroethane \u662f insert_one<\/p>\n<p>@caly5144 client.db \u597d\u50cf\u662f\u53ef\u4ee5\uff0ccollection \u6211\u4e5f\u662f\u4e60\u60ef\u7528 db[collection]\uff0c\u4e0d\u8fc7 lz \u662f\u4e0d\u662f\u5c11\u4e86\u4e00\u7ea7\uff1f client.db.collection                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"682152\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Nitroethane <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @JCZ2MkKb5S8ZX9pq #4 <br \/>@Colorful #2 <\/p>\n<p>\u884c\u4e0e\u4e0d\u884c\u5012\u662f\u628a\u8fd4\u56de\u503c\u6253\u5370\u51fa\u6765\uff0c\u6709 exception \u629b\u51fa\u7684\u8bdd\u628a exception \u4e5f\u8d34\u51fa\u6765\uff0c\u4f60\u4e00\u53e5\u4e5f\u4e0d\u884c\u8c01\u77e5\u9053\u662f\u4ec0\u4e48\u95ee\u9898                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"682153\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : littlespider89 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             client.db.newtext.insert_one({&#8230;})                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"682154\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JCZ2MkKb5S8ZX9pq <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Nitroethane \u6211\u770b\u4ee3\u7801\u597d\u50cf\u662f\u5c11\u4e86\u4e00\u7ea7\uff0c\u6ca1\u6709 collection\uff0c\u76f4\u63a5\u5728 db.insert\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"682155\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Colorful <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @JCZ2MkKb5S8ZX9pq \u5bf9\u7684\uff0c\u627e\u5230\u539f\u56e0\u4e86\uff0c\u5c31\u662f\u5c11\u4e86\u4e00\u6781\u7684\u539f\u56e0\uff0c\u592a\u611f\u8c22\u5404\u4f4d\u5927\u4fa0\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e0b\uff0c Python \u91cc\u63d2\u5165 m&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/102517"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=102517"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/102517\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=102517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=102517"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=102517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}