Python flask ValueError:对关闭的文件执行I/O操作

Python flask ValueError:对关闭的文件执行I/O操作,python,flask,Python,Flask,我有一个代码,我正在使用它从网页上刮取数据,我将刮取的数据保存在一个html文件中,并将其显示为另一个页面。下面是代码 from flask import Flask, render_template,request from bs4 import BeautifulSoup import urllib.request import sys,os app = Flask(__name__) @app.route('/') def index(): return render_templa

我有一个代码,我正在使用它从网页上刮取数据,我将刮取的数据保存在一个html文件中,并将其显示为另一个页面。下面是代码

from flask import Flask, render_template,request from bs4 import
BeautifulSoup import urllib.request import sys,os app =
Flask(__name__) @app.route('/') def index():
    return render_template ('index.html')

@app.route('/result',methods = ['POST']) def result():    if
request.method == 'POST':
      result = request.form.get("search")
      link = "https://xyz.comindex?search="
      url = (link+result)
      print(url)
      try:
         page = urllib.request.urlopen(url)
         soup = BeautifulSoup(page, 'html.parser')
         test = soup.findAll('div', attrs={"class": "search-inner-wrapper"})
         sys.stdout = open("tests.html", "w")
         print(test)
         sys.stdout.close()
         return render_template("SearchResults.html", test=test)
      except:
         print("An error occured.")
      return render_template("test.html", test=test)


if __name__ == '__main__':
    app.run(use_reloader = True, debug = True)
我的问题是,当我重新加载索引页并执行我得到的搜索查询时,这段代码工作得非常好,但只有一次

ValueError:对关闭的文件执行I/O操作


我无法找到解决方法,因为我每次都必须使用单个文件,并且不希望结果附加现有代码。

您正在将sys.stdout重新定义为您打开的文件的文件句柄。使用其他名称,不要覆盖sys.stdout。不要关闭sys.stdout。但是关闭您创建的文件句柄是可以的

打开并读取文件、打开并写入文件的示例:

bjb@blueeyes:~$ cat /tmp/infile.html
<html>
<head>
</head>
<body>
<div class="search-inner-wrapper">fleeble flobble</div>
</body>
</html>
bjb@blueeyes:~$ cat /tmp/file2.py
#!/usr/bin/env python3


with open('/tmp/infile.html', 'r') as infile:
    page = infile.readlines()

with open('/tmp/outfile.html', 'w') as ofd:
    ofd.write(''.join(page))


bjb@blueeyes:~$ /tmp/file2.py
bjb@blueeyes:~$ cat /tmp/outfile.html
<html>
<head>
</head>
<body>
<div class="search-inner-wrapper">fleeble flobble</div>
</body>
</html>

希望这能在flask脚本中运行…

您正在将sys.stdout重新定义为您打开的文件的文件句柄。使用其他名称,不要覆盖sys.stdout。不要关闭sys.stdout。但是关闭您创建的文件句柄是可以的

打开并读取文件、打开并写入文件的示例:

bjb@blueeyes:~$ cat /tmp/infile.html
<html>
<head>
</head>
<body>
<div class="search-inner-wrapper">fleeble flobble</div>
</body>
</html>
bjb@blueeyes:~$ cat /tmp/file2.py
#!/usr/bin/env python3


with open('/tmp/infile.html', 'r') as infile:
    page = infile.readlines()

with open('/tmp/outfile.html', 'w') as ofd:
    ofd.write(''.join(page))


bjb@blueeyes:~$ /tmp/file2.py
bjb@blueeyes:~$ cat /tmp/outfile.html
<html>
<head>
</head>
<body>
<div class="search-inner-wrapper">fleeble flobble</div>
</body>
</html>

希望这能在flask脚本中工作…

如果我不关闭它,它会将废弃的结果写入现有文件中。我不能在这里使用多线程或某种循环吗?您正在打开一个文件进行写入-该部分正常-但您正在将打开的文件描述符分配给sys.stdout-该部分不正常。将open()的输出指定给另一个名称,然后关闭另一个名称。保持sys.stdout打开-不要关闭它。如果您关闭它,您将无法再次写入标准输出。如果我不关闭它,它将在现有文件中写入并附加废弃的结果。我不能在这里使用多线程或某种循环吗?您正在打开一个文件进行写入-该部分正常-但您正在将打开的文件描述符分配给sys.stdout-该部分不正常。将open()的输出指定给另一个名称,然后关闭另一个名称。保持sys.stdout打开-不要关闭它。如果关闭它,您将无法再次写入标准输出。我不明白您为什么认为需要在此处重写
sys.stdout
。使用您最喜欢的搜索引擎查找如何用Python写入文件。我不明白您为什么认为需要在此处覆盖
sys.stdout
。使用您喜爱的搜索引擎查找如何用Python编写文件。