{"id":183,"date":"2020-07-27T07:53:04","date_gmt":"2020-07-26T23:53:04","guid":{"rendered":"http:\/\/www.gaoxigang.com\/?p=183"},"modified":"2020-07-27T07:53:04","modified_gmt":"2020-07-26T23:53:04","slug":"scrapy-%e7%88%ac%e8%99%ab%e6%a1%86%e6%9e%b6-py%e5%ae%9e%e7%8e%b0%e5%a2%9e%e3%80%81%e5%88%a0%e3%80%81%e6%9f%a5%e3%80%81%e6%94%b9","status":"publish","type":"post","link":"https:\/\/www.gaoxigang.com\/index.php\/2020\/07\/27\/scrapy-%e7%88%ac%e8%99%ab%e6%a1%86%e6%9e%b6-py%e5%ae%9e%e7%8e%b0%e5%a2%9e%e3%80%81%e5%88%a0%e3%80%81%e6%9f%a5%e3%80%81%e6%94%b9\/","title":{"rendered":"Scrapy \u722c\u866b\u6846\u67b6-py\u5b9e\u73b0\u589e\u3001\u5220\u3001\u67e5\u3001\u6539"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u6570\u636e\u63d2\u5165\u64cd\u4f5c<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">try:\n    sql = \"insert into article (title, name, url, collection) values (%s,%s,%s,%s)\"\n    # \u901a\u8fc7\u6e38\u6807\u5b8c\u6210CRUD\n    cursor.execute(sql,('\u6211\u662f\u6807\u9898','\u4f5c\u8005\u540d\u79f0','url\u5730\u5740','\u6587\u827a\u3001\u79d1\u6280\u3001\u5a31\u4e50'))\n    # \u9ed8\u8ba4\u9700\u8981\u624b\u52a8\u63d0\u4ea4\u4e8b\u52a1\n    db.commit()\nexcept:\n    print('sql\u63d2\u5165\u51fa\u9519')\n    db.rollback()\nfinally:\n    cursor.close()\n    db.close()\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6570\u636e\u66f4\u65b0\u64cd\u4f5c<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cursor = db.cursor()\n# \u6570\u636e\u7684\u63d2\u5165\u64cd\u4f5c  db  file url \u90fd\u9700\u8981\u5f02\u5e38\u7684\u6355\u83b7\ntry:\n    sql = \"update article set title=%s, name=%s where id=%s\"\n    # \u8fd4\u56de\u7684\u662f\u53d7\u5f71\u54cd\u7684\u884c\u6570\n    count = cursor.execute(sql,('\u65b0\u6807\u98982','\u8001\u4f5c\u80052',3))\n    print('\u66f4\u65b0\u7684\u884c\u6570\u4e3a',count)\n    # \u9ed8\u8ba4\u9700\u8981\u624b\u52a8\u63d0\u4ea4\u4e8b\u52a1\n    db.commit()\nexcept:\n    print('sql\u66f4\u65b0\u51fa\u9519')\n    db.rollback()\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6570\u636e\u67e5\u8be2\u64cd\u4f5c<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cursor = db.cursor()\n# \u6570\u636e\u7684\u63d2\u5165\u64cd\u4f5c  db  file url \u90fd\u9700\u8981\u5f02\u5e38\u7684\u6355\u83b7\ntry:\n    sql = \"select * from article where id= %s \"\n    # \u8fd4\u56de\u7684\u662f\u53d7\u5f71\u54cd\u7684\u884c\u6570\n    count = cursor.execute(sql,3)\n    print('\u67e5\u8be2\u7684\u8bb0\u5f55\u6570\u4e3a',count)\n    # \u67e5\u8be2\u4e0d\u9700\u8981\u63d0\u4ea4\u4e8b\u52a1,\u4f46\u662f\u8981\u901a\u8fc7fetch\u5b8c\u6210\u6570\u636e\u7684\u6293\u53d6\n    print(cursor.fetchone())\nexcept:\n    print('sql\u67e5\u8be2\u51fa\u9519')\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u67e5\u8be2\u5206\u9875\u5b9e\u73b0<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cursor = db.cursor()\ntry:\n    keyword = '\u6807\u9898'\n    current_page = 1\n    size = 3\n    sql = \"select * from article where title like %s limit %s,%s\"\n    # \u8fd4\u56de\u7684\u662f\u53d7\u5f71\u54cd\u7684\u884c\u6570\n    count = cursor.execute(sql,(f'%{keyword}%',(current_page-1)*size,size))\n    print('\u67e5\u8be2\u7684\u8bb0\u5f55\u6570\u4e3a',count)\n    # \u67e5\u8be2\u4e0d\u9700\u8981\u63d0\u4ea4\u4e8b\u52a1,\u4f46\u662f\u8981\u901a\u8fc7fetch\u5b8c\u6210\u6570\u636e\u7684\u6293\u53d6\n    for row in cursor.fetchall():\n        print(row)\nexcept:\n    print('sql\u67e5\u8be2\u5206\u9875\u51fa\u9519')\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u63d2\u5165\u64cd\u4f5c \u6570\u636e\u66f4\u65b0\u64cd\u4f5c \u6570\u636e\u67e5\u8be2\u64cd\u4f5c \u67e5\u8be2\u5206\u9875\u5b9e\u73b0<\/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":[],"class_list":["post-183","post","type-post","status-publish","format-standard","hentry","category-biji"],"_links":{"self":[{"href":"https:\/\/www.gaoxigang.com\/index.php\/wp-json\/wp\/v2\/posts\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gaoxigang.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gaoxigang.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gaoxigang.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gaoxigang.com\/index.php\/wp-json\/wp\/v2\/comments?post=183"}],"version-history":[{"count":0,"href":"https:\/\/www.gaoxigang.com\/index.php\/wp-json\/wp\/v2\/posts\/183\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gaoxigang.com\/index.php\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gaoxigang.com\/index.php\/wp-json\/wp\/v2\/categories?post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gaoxigang.com\/index.php\/wp-json\/wp\/v2\/tags?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}