#python #html #flask #jqwidget
Вопрос:
Я написал простое приложение для колбы с пакетом PyQt5 QWebEnginePage. У меня есть кнопка на index.html
странице. Когда я нажму на кнопку, она получит текст Google.com
и отобразит его на следующей странице. Я использую Красивый Суп, чтобы получить текст.
Проблема в том, что с первого раза все работает нормально. Но если я вернусь и снова нажму кнопку, чтобы получить текст от Google, он вылетит с этим сообщением в браузере.
Вот мои коды.
app.py
from flask import Flask, render_template
from QApp import *
app = Flask(__name__)
@app.route('/')
def main():
return render_template('index.html')
@app.route('/runtest')
def run_test():
data = run_qapp()
return render_template("Qapp_test.html", data=data)
if __name__ == '__main__':
app.run(debug=True)
QApp.py
from bs4 import BeautifulSoup
import requests
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl, QCoreApplication
from PyQt5.QtWebEngineWidgets import QWebEnginePage
import sys
class Page(QWebEnginePage):
def __init__(self, url):
self.app = QCoreApplication.instance()
if self.app is None:
self.app = QApplication(sys.argv)
QWebEnginePage.__init__(self)
self.html = ''
self.loadFinished.connect(self._on_load_finished)
self.load(QUrl(url))
self.app.exec_()
def _on_load_finished(self):
self.html = self.toHtml(self.Callable)
def Callable(self, html_str):
self.html = html_str
self.app.quit()
def run_qapp():
page = Page('https://www.google.com/')
soup = BeautifulSoup(page.html, 'html.parser')
return soup.text
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/runtest">
<input type="submit" value="Get Google Source">
</form>
</body>
</html>
Qapp_test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ data }}
</body>
</html>
So the first time I click the button, it works, the text is being shown in the next page. This is the terminal message
127.0.0.1 - - [18/Mar/2021 18:14:15] "?[37mGET / HTTP/1.1?[0m" 200 -
WARNING: QApplication was not created in the main() thread.
[4364:16952:0318/181419.539:ERROR:cache_util_win.cc(21)] Unable to move the cache: Access is denied. (0x5)
[4364:16952:0318/181419.539:ERROR:cache_util.cc(139)] Unable to move cache folder C:UsersabidhAppDataLocalpythonQtWebEngine
DefaultGPUCache to C:UsersabidhAppDataLocalpythonQtWebEngineDefaultold_GPUCache_000
[4364:16952:0318/181419.539:ERROR:disk_cache.cc(184)] Unable to create cache
[4364:16952:0318/181419.539:ERROR:shader_disk_cache.cc(606)] Shader Cache Creation failed: -2
[4364:16952:0318/181422.557:ERROR:service_worker_storage.cc(1575)] Failed to delete the database: Database IO error
127.0.0.1 - - [18/Mar/2021 18:14:25] "?[37mGET /runtest HTTP/1.1?[0m" 200 -
Затем я нажимаю кнопку «Назад» в браузере и нажимаю кнопку, чтобы снова получить текст, и приложение выходит из строя. Это сообщение я получаю в терминале
WARNING: QApplication was not created in the main() thread.
Можете ли вы помочь мне выяснить, что не так и что нужно исправить? Заранее спасибо.