{"id":192293,"date":"2022-04-28T08:15:27","date_gmt":"2022-04-28T00:15:27","guid":{"rendered":"https:\/\/www.keaglegz.com\/192293.html"},"modified":"2026-07-14T16:16:14","modified_gmt":"2026-07-14T08:16:14","slug":"%e4%bd%bf%e7%94%a8-python-%e8%87%aa%e5%8a%a8%e7%88%ac%e5%8f%96%e7%bd%91%e9%a1%b5%e5%b0%8f%e8%af%b4%e5%b9%b6%e7%94%9f%e6%88%90-txt-%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/192293.html","title":{"rendered":"\u4f7f\u7528 Python \u81ea\u52a8\u722c\u53d6\u7f51\u9875\u5c0f\u8bf4\u5e76\u751f\u6210 TXT \u6587\u4ef6"},"content":{"rendered":"\n<p>\u7814\u7a76\u8fd9\u4e2a\u7eaf\u7cb9\u662f\u56e0\u4e3a\u7f51\u53cb\u95ee\u300a\u5251\u6765\u300bTXT \u7cbe\u6821\u7248\u5565\u65f6\u5019\u66f4\u65b0\u800c\u7814\u7a76\u7684\uff0c\u4e4b\u524d\u5df2\u7ecf\u5c1d\u8bd5\u4f7f\u7528 TextForever \u8f6f\u4ef6\u5c06 HTML \u7f51\u9875\u5c0f\u8bf4\u6279\u91cf\u4e00\u952e\u8f6c\u6362\u6210 TXT \u683c\u5f0f\uff0c\u5948\u4f55\u73b0\u5728\u7684\u7f51\u9875\u5c0f\u8bf4\u5728\u6b63\u6587\u5185\u4e5f\u52a0\u5165\u4e86\u5e7f\u544a\uff0c\u6709\u4e9b\u751a\u81f3\u8fd8\u81ea\u52a8\u5206\u9875\u3002\u540e\u671f\u6392\u7248\u9ebb\u70e6\u5e76\u4e14\u5361\u987f\uff0c\u81f3\u5c11\u7f19\u54e5\u54e5\u7684\u73b0\u5728\u7684\u7b14\u8bb0\u672c\u7535\u8111\u8fd8\u662f\u6bd4\u8f83\u5361\u7684\uff0c\u73b0\u5728\u4e0d\u662f Python \u6d41\u884c\u561b\uff0c\u6b63\u597d\u4f7f\u7528 Python \u81ea\u52a8\u722c\u53d6\u7f51\u9875\u5c0f\u8bf4\u5e76\u751f\u6210 TXT \u6587\u4ef6\u3002<\/p>\n<p><a href=\"https:\/\/www.dongwubaike.cn\/fanhao\/wp-content\/uploads\/2024\/06\/hao2022223233.jpg\" data-fancybox=\"images\"><img decoding=\"async\" src=\"https:\/\/www.dongwubaike.cn\/fanhao\/wp-content\/uploads\/2024\/06\/hao2022223233.jpg\" alt=\"\u4f7f\u7528 Python \u81ea\u52a8\u722c\u53d6\u7f51\u9875\u5c0f\u8bf4\u5e76\u751f\u6210 TXT \u6587\u4ef6\" \/><\/a><\/p>\n<h2>\u5b89\u88c5 Python \u73af\u5883<\/h2>\n<p>1\u3001\u4e0b\u8f7d Python3.7.3 \u6e90\u7801\u5305<\/p>\n<pre>wget -c https:\/\/www.python.org\/ftp\/python\/3.7.3\/Python-3.7.3.tgz<\/pre>\n<p>2\u3001\u89e3\u538b Python3.7.3 \u6e90\u7801\u5305<\/p>\n<pre>tar -xzvf Python-3.7.3.tgz<\/pre>\n<p>3\u3001\u8fdb\u5165 Python3.7.3 \u76ee\u5f55<\/p>\n<pre>cd Python-3.7.3<\/pre>\n<p>4\u3001\u914d\u7f6e\u5b89\u88c5\u4fe1\u606f<\/p>\n<pre>.\/configure --with-ssl<\/pre>\n<p>\u201c\u8fd0\u6c14\u4e0d\u597d\u201d\u7684\u5c0f\u4f19\u4f34\u53ef\u80fd\u4f1a\u9047\u5230\u4ee5\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre>configure: error: in `\/root\/Python-3.7.3':  configure: error: no acceptable C compiler found in $PATH  <\/pre>\n<p>\u8be5\u9519\u8bef\u89e3\u51b3\u529e\u6cd5\uff1a\u5b89\u88c5 GCC \u8f6f\u4ef6\u5957\u4ef6<\/p>\n<pre>yum install gcc<\/pre>\n<p>\u7136\u540e\u91cd\u65b0\u6267\u884c\u914d\u7f6e\u5b89\u88c5\u4fe1\u606f\u5373\u53ef\u3002<\/p>\n<p>5\u3001\u5b89\u88c5 openssl-devel \u652f\u6301<\/p>\n<pre>yum install openssl-devel<\/pre>\n<p>\u7f16\u8bd1\u5e76\u5b89\u88c5<\/p>\n<pre>make &amp;&amp; make install<\/pre>\n<p>6\u3001\u8fd9\u65f6\u5019\uff0c\u4f60\u4f1a\u5e78\u8fd0\u7684\u53d1\u73b0\uff0c\u53c8TM\u51fa\u9519\u4e86\uff1a<\/p>\n<pre>File \"\/root\/Python-3.7.3\/Lib\/ctypes\/__init__.py\", line 7, in &lt;module&gt;  from _ctypes import Union, Structure, Array  ModuleNotFoundError: No module named '_ctypes'  make: *** [install] \u9519\u8bef 1<\/pre>\n<p>\u539f\u56e0\u662f\u7f3a\u5c11\u4f9d\u8d56\u5305\uff0c\u5b89\u88c5 libffi \u4f9d\u8d56\u5373\u53ef\u3002<\/p>\n<pre>yum install libffi-devel -y<\/pre>\n<p>\u8fd9\u4e2a\u89e3\u51b3\u540e\u91cd\u65b0\u8fdb\u884c\u7b2c 6 \u6b65\u7f16\u8bd1\u5b89\u88c5\u3002\u4e0d\u51fa\u610f\u5916\uff0c\u4f1a\u51fa\u73b0<code>Successfully installed<\/code>\u8fd9\u4e2a\u5b89\u88c5\u6210\u529f\u7684\u63d0\u793a\uff01<\/p>\n<h2>\u6267\u884c Python \u811a\u672c\u722c\u5c0f\u8bf4<\/h2>\n<p>\u7531\u4e8e\u8be5\u811a\u672c\u4f7f\u7528\u4e86 Python \u6269\u5c55\u5e93\uff0c\u8bf7\u5148\u5b89\u88c5<code>BeautifulSoup<\/code>\u4e0e<code>requests<\/code>\u652f\u6301\u3002<\/p>\n<pre>pip3 install beautifulsoup4  pip3 install requests<\/pre>\n<p>\u7136\u540e\u4efb\u610f\u521b\u5efa\u4e2a\u6587\u4ef6\u5939\uff08\u6216\u8005\u76f4\u63a5\u5728\u6839\u76ee\u5f55\uff09\u653e\u5165 17549.py \u811a\u672c\u3002<\/p>\n<p>\u8f93\u5165<code>python3 17549.py<\/code>\uff08\u5982\u679c\u521b\u5efa\u4e86\u4e2a\u6587\u4ef6\u5939\uff0c\u8bb0\u5f97\u5148 cd \u8fdb\u53bb\uff09\u5c31\u5f00\u59cb\u722c\u4e86\u2026\u2026<\/p>\n<h3>Python \u722c\u7f51\u9875\u5c0f\u8bf4\u811a\u672c<\/h3>\n<p>\u4e3a\u4e86\u65b9\u4fbf\u5927\u5bb6\u4f7f\u7528\uff0c\u8be5\u811a\u672c\u5df2\u7ecf\u6253\u5305\u653e\u7f51\u76d8\uff0c\u53ef\u4ee5\u76f4\u63a5\u4e0b\u8f7d\u4f7f\u7528\u3002<\/p>\n<p>\u722c\u5c0f\u8bf4\u751f\u6210 TXT \u793a\u4f8b Python \u811a\u672c\u4e0b\u8f7d: http:\/\/ct.dujin.org\/f\/5210373-485800393-60f362<\/p>\n<p>\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre># -*- coding:UTF-8 -*-  from bs4 import BeautifulSoup  import requests  import sys  import time    class downloader(object):    def __init__(self,url):  self.target = url # \u7ae0\u8282\u9875  self.names = [] # \u5b58\u653e\u7ae0\u8282\u540d  self.urls = [] # \u5b58\u653e\u7ae0\u8282\u94fe\u63a5  self.nums = 0 # \u7ae0\u8282\u6570  self.title=\"\"#\u5c0f\u8bf4\u540d    def get_one_text(self, url_i):    text = ' '  url_i=\"https:\/\/www.nitianxieshen.com\"+url_i  r = requests.get(url=url_i)  r.encoding = r.apparent_encoding    html = r.text  html_bf = BeautifulSoup(html, features='html.parser')  #div = html_bf.find_all('div', attrs={\"id\":\"content\"})  #print(div.find('div',attrs={\"class\":\"m-tpage\"}))  texts=html_bf.find_all('p')  texts[0].decompose()  texts[len(texts)-1].decompose()  for t in texts:  text += str(t)  text = text.replace('&lt;None&gt;', '')  text = text.replace('&lt;\/None&gt;', '')  text = text.replace('&lt;\/div&gt;', 'n')  text = text.replace('&lt;br\/&gt;', 'n')  text = text.replace('&lt;p&gt;', 'n')  text = text.replace('&lt;\/p&gt;', 'n')  text = text.replace('&lt;p&gt;', 'n')    return text    def get_name_address_list(self):  list_a_bf = []  list_a = []  r = requests.get(self.target)  r.encoding = r.apparent_encoding  html = r.text  div_bf = BeautifulSoup(html, features='html.parser')  self.title=div_bf.find('h1').text  div = div_bf.find_all('div',attrs={\"id\":\"play_0\"})[0]  li=div.find_all('li')  self.nums=len(li)  for i in range(len(li)):  self.names.append(li[i].find('a').string) # string\u65b9\u6cd5\u8fd4\u56de\u7ae0\u8282\u540d  self.urls.append(li[i].find('a').get('href')) # get\uff08\u2018href\u2019\uff09\u8fd4\u56de\u5b50\u5730\u5740\u4e32  #print(self.names)  #print(self.urls)  print(\"\u5171\uff1a\"+str(self.nums)+\"\u7ae0\")    def writer(self, name, path, text):  write_flag = True  with open(path, 'a', encoding='utf-8') as f: # \u6253\u5f00\u76ee\u6807\u8def\u5f84\u6587\u4ef6  f.write(name + 'n')  f.writelines(text)  f.write('nn')    \u00a0    if __name__ == \"__main__\":    dl = downloader(\"https:\/\/www.nitianxieshen.com\/zhuxian\/\")  dl.get_name_address_list()    print('\u300a'+dl.title+'\u300b\u5f00\u59cb\u4e0b\u8f7d\uff1a')  for i in range(dl.nums):  time.sleep(0.2)  try:  dl.writer(dl.names[i], r''+dl.title+'.txt', dl.get_one_text(dl.urls[i]))  except IndexError as e:  print(repr(e))  sys.stdout.write(\" \u5df2\u4e0b\u8f7d:%.3f%%\" % float((i\/dl.nums)*100) + 'r'+'\u5f53\u524d\u7b2c\uff1a'+str(i)+' \u7ae0')  sys.stdout.flush()  print(dl.title+'\u4e0b\u8f7d\u5b8c\u6210')<\/pre>\n<p>\u60f3\u628a\u5176\u4ed6\u7f51\u9875\u5c0f\u8bf4\u4fdd\u5b58\u4e3a\u5355\u4e2a TXT \u6587\u4ef6\uff0c\u53ea\u9700\u8981\u4fee\u6539\u5012\u6570\u7b2c\u4e8c\u6bb5\u7684\u5c0f\u8bf4\u76ee\u5f55\u5730\u5740\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7814\u7a76\u8fd9\u4e2a\u7eaf\u7cb9\u662f\u56e0\u4e3a\u7f51\u53cb\u95ee\u300a\u5251\u6765\u300bTXT \u7cbe\u6821\u7248\u5565\u65f6\u5019\u66f4\u65b0\u800c\u7814\u7a76\u7684\uff0c\u4e4b\u524d\u5df2\u7ecf\u5c1d\u8bd5\u4f7f\u7528 TextForever \u8f6f\u4ef6\u5c06 HTML \u7f51\u9875\u5c0f\u8bf4\u6279\u91cf\u4e00\u952e\u8f6c\u6362\u6210 TXT \u683c\u5f0f\uff0c\u5948\u4f55\u73b0\u5728\u7684\u7f51\u9875\u5c0f\u8bf4\u5728\u6b63\u6587\u5185\u4e5f\u52a0\u5165\u4e86\u5e7f\u544a\uff0c\u6709\u4e9b\u751a\u81f3\u8fd8\u81ea\u52a8\u5206\u9875\u3002\u540e\u671f\u6392\u7248\u9ebb\u70e6\u5e76\u4e14\u5361\u987f\uff0c\u81f3\u5c11\u7f19\u54e5\u54e5\u7684\u73b0\u5728\u7684\u7b14\u8bb0\u672c\u7535\u8111\u8fd8\u662f\u6bd4\u8f83\u5361\u7684\uff0c\u73b0\u5728\u4e0d\u662f Python \u6d41\u884c\u561b\uff0c\u6b63\u597d\u4f7f\u7528 Python \u81ea\u52a8\u722c\u53d6\u7f51\u9875\u5c0f\u8bf4\u5e76\u751f\u6210 TXT \u6587\u4ef6\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[10316,96416],"class_list":["post-192293","post","type-post","status-publish","format-standard","hentry","tag-eature","tag-ile"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/192293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/comments?post=192293"}],"version-history":[{"count":1,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/192293\/revisions"}],"predecessor-version":[{"id":307171,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/192293\/revisions\/307171"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=192293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=192293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=192293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}