{"id":98739,"date":"2020-04-03T10:47:38","date_gmt":"2020-04-03T02:47:38","guid":{"rendered":"http:\/\/4563.org\/?p=98739"},"modified":"2020-04-03T10:47:38","modified_gmt":"2020-04-03T02:47:38","slug":"%e6%96%b0%e4%ba%ba%e6%b1%82%e8%a7%a3-python-%e5%86%99%e5%85%a5-txt-%e6%97%b6%e6%95%b0%e6%8d%ae%e4%b8%a2%e5%a4%b1%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=98739","title":{"rendered":"\u65b0\u4eba\u6c42\u89e3: Python \u5199\u5165 txt \u65f6\u6570\u636e\u4e22\u5931\u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u65b0\u4eba\u6c42\u89e3: Python \u5199\u5165 txt \u65f6\u6570\u636e\u4e22\u5931\u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chaneyccy <\/span>  <span><i><\/i> 55<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4e3b\u8981\u95ee\u9898\u5728\u8fd9\uff1a<\/p>\n<p>for txt in list_: txts = txt.get_text() #\u5728\u8fd9\u91cc print(txts)\uff0c \u7ed3\u679c\u8fd8\u662f\u5b8c\u6574\u7684 download_run(title1, title2, title3, title4, txts) #\u5728\u8fd9\u91cc print(txts)\uff0c \u6570\u636e\u53ea\u5269\u6700\u540e\u4e00\u4e2a\u6bb5\u843d\u4e86<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a def download(href_urls): for url in href_urls: mod_titles = [] ses = requests.session() html = ses.get(url, headers = header(), verify = False) soup = BeautifulSoup(html.content, &#8216;html.parser&#8217;) title_list = soup.find(class_ = &#8216;g-ctnBar&#8217;).find_all(&#8216;a&#8217;) title1 = title_list[2].get_text() title2 = title_list[3].get_text() title3 = title_list[4].get_text() title4 = title_list[5].get_text() list_ = soup.find_all(&#8216;div&#8217;, class_ = &#8216;detail-mod J_floor&#8217;)[:-3] for txt in list_: txts = txt.get_text() download_run(title1, title2, title3, title4, txts)<\/p>\n<p>def download_run(title1, title2, title3, title4, txts): path = &#8216;C:\/Users\/Desktop\/run\/%s\/%s\/%s&#8217; %(title1, title2, title3) if not os.path.exists(path): os.makedirs(path) with open(&#8216;C:\/Users\/Desktop\/run\/%s\/%s\/%s\/%s.txt&#8217; %(title1, title2, title3, title4), &#8216;w&#8217;)as f: f.write(txts)<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>18<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"727157\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chaneyccy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6392\u7248\u6709\u70b9\u4e71\uff0c\u66f4\u65b0\u4e00\u4e0b<\/p>\n<p>def download(href_urls): <\/p>\n<p> for url in href_urls: <\/p>\n<p> mod_titles = [] <\/p>\n<p> ses = requests.session() <\/p>\n<p> html = ses.get(url, headers = header(), verify = False) <\/p>\n<p> soup = BeautifulSoup(html.content, &#8216;html.parser&#8217;) <\/p>\n<p> title_list = soup.find(class_ = &#8216;g-ctnBar&#8217;).find_all(&#8216;a&#8217;) <\/p>\n<p> title1 = title_list[2].get_text() <\/p>\n<p> title2 = title_list[3].get_text() <\/p>\n<p> title3 = title_list[4].get_text() <\/p>\n<p> title4 = title_list[5].get_text() <\/p>\n<p> list_ = soup.find_all(&#8216;div&#8217;, class_ = &#8216;detail-mod J_floor&#8217;)[:-3] <\/p>\n<p> for txt in list_:<\/p>\n<p> txts = txt.get_text() <\/p>\n<p> download_run(title1, title2, title3, title4, txts)<\/p>\n<p>def download_run(title1, title2, title3, title4, txts): <\/p>\n<p> path = &#8216;C:\/Users\/Desktop\/run\/%s\/%s\/%s&#8217; %(title1, title2, title3) <\/p>\n<p> if not os.path.exists(path): <\/p>\n<p> os.makedirs(path) <\/p>\n<p> with open(&#8216;C:\/Users\/Desktop\/run\/%s\/%s\/%s\/%s.txt&#8217; %(title1, title2, title3, title4), &#8216;w&#8217;)as f:<\/p>\n<p> f.write(txts)                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727158\" 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>                                                             &#8220;`<br \/>\u4f60\u7684\u4ee3\u7801<br \/>&#8220;`<\/p>\n<p>\u8fd9\u6837\u53ef\u4ee5\u4fdd\u7559\u6a21\u5f0f\u3002\u56de\u590d\u65f6\u65e0\u6548\u3002\u8be6\u60c5\u53ef\u67e5 markdown \u683c\u5f0f\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727159\" 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>                                                             \u683c\u5f0f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727160\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chaneyccy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @JCZ2MkKb5S8ZX9pq \u597d\u7684\uff0c\u5e73\u65f6\u6ca1\u6709\u7528 markdown \u5199\u5185\u5bb9\u7684\u4e60\u60ef~ \u6211\u53bb\u7814\u7a76\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727161\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cxyfreedom <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u904d\u5386\u5faa\u73af\u53c8\u6bcf\u6b21\u5199\u5165\uff0c\u4f60\u5faa\u73af\u5b8c\u6210\u540e\uff0ctxts \u672c\u6765\u5c31\u53ea\u6709\u4e00\u90e8\u5206\u7684\u6570\u636e\uff0c\u5199\u5165\u5230\u6587\u4ef6\u4e2d\u5f53\u7136\u5c31\u53ea\u6709\u6700\u540e\u4e00\u90e8\u5206\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727162\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/www.runoob.com\/python\/python-func-open.html<br \/>\u770b\u8868\u683c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727163\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cxyfreedom #5 \u4ee3\u7801\u6211\u8bfb\u4e86\uff0c\u662f\u56e0\u4e3a\u6bcf\u6b21\u90fd open(file,&#8217;w&#8217;)\u5199\u5165\u7684\u539f\u56e0\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727164\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Wuuuu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u5e94\u8be5\u662f txts = txts+&#8221;n&#8221;+txt.get_txt()?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727165\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Wuuuu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Vegetable py \u9760\u7f29\u8fdb\u2026\u2026\u8fd9\u6837\u7684\u4e0d\u77e5\u9053\u5230\u5e95\u662f<br \/>for txt in list_:<\/p>\n<p> txts = txt.get_text()<\/p>\n<p> download_run(title1, title2, title3, title4, txts)<\/p>\n<p>\u8fd8\u662f<br \/>for txt in list_:<\/p>\n<p> txts = txt.get_text()<\/p>\n<p>download_run(title1, title2, title3, title4, txts)<\/p>\n<p>\u4f46\u5927\u6982\u7387\u662f\u7b2c\u4e8c\u79cd\u5199\u6cd5\u5427\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727166\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Wuuuu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             for txt in list_:<\/p>\n<p>t txts = txt.get_text()<\/p>\n<p>t download_run(title1, title2, title3, title4, txts)<\/p>\n<p>for txt in list_:<\/p>\n<p>t txts = txt.get_text()<\/p>\n<p>download_run(title1, title2, title3, title4, txts)                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727167\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : LiuSha <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a w \u6539\u6210 a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727168\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhejiangblue <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6253\u5f00\u6a21\u5f0f\u7528 a\uff0cw \u4f1a\u91cd\u65b0\u521b\u5efa\u6587\u4ef6\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727169\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chaneyccy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Vegetable \u611f\u8c22\u611f\u8c22\uff0c\u4e4b\u524d\u628a\u4ece&#8217;a&#8217;\u6539\u6210&#8217;w&#8217;\u4e86\uff0c\u6ca1\u8003\u8651\u5230\u8fd9\u4e2a\u539f\u56e0\uff0c\u8c03\u56de\u6765\u6b63\u5e38\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727170\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cxyfreedom <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Vegetable \u5982\u679c for \u5faa\u73af\u4e0b\u9762\u7684\u8bdd\u90a3\u5c31\u662f\u8fd9\u4e2a\u95ee\u9898\u3002\u8ffd\u52a0\u5199\u5165\u4e0d\u80fd\u7528 w\u3002\u6309\u8fd9\u4e2a\u4ee3\u7801\u903b\u8f91\u4e0d\u5982\u4e00\u6b21\u6027\u5199\u5165\u5462                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727171\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : vincexu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             ![image.png]( https:\/\/i.loli.net\/2020\/01\/14\/HBUCoWMXdINqpzF.png)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727172\" 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>                                                             \u5982\u679c\u662f aw \u7684\u95ee\u9898 \u5efa\u8bae\u62fc\u63a5 txt \u7136\u540e w<br \/>a \u4e07\u4e00\u91cd\u590d\u6267\u884c\u4f1a\u91cd\u590d\u5199\u5165                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727173\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cherbim <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u6587\u4ef6\u5199\u5165\u7684\u662f w\uff0c\u6bcf\u6b21\u90fd\u4f1a\u91cd\u5199\uff0c\u7528 a \u8ffd\u52a0\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"727174\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : shunfa52000 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0a\u6b21\u6211\u7a0b\u5e8f\u7ed3\u5c3e\u6ca1\u6709\u52a0 f.close,\u7ed3\u679c\u5faa\u73af\u6b21\u6570\u591a\u7684\u65f6\u5019\u62a5\u9519\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u65b0\u4eba\u6c42\u89e3: Python \u5199\u5165 t&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\/98739"}],"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=98739"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/98739\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=98739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=98739"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=98739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}