如何解决Requests-html:在烧瓶上运行时出错
我已经准备好使用requests-html
的脚本,并且运行良好。
我将其部署在flask应用程序中,现在它给了我RuntimeError: There is no current event loop in thread 'Thread-3'.
这是完整的错误:
Traceback (most recent call last):
File "C:\Users\intel\AppData\Local\Programs\Python\python38\Lib\site-packages\flask\app.py",line 2464,in __call__
return self.wsgi_app(environ,start_response)
.
.
.
File "C:\Users\intel\Desktop\One page\main.py",line 18,in hello_world
r.html.render()
File "C:\Users\intel\AppData\Local\Programs\Python\python38\Lib\site-packages\requests_html.py",line 586,in render
self.browser = self.session.browser # Automatically create a event loop and browser
File "C:\Users\intel\AppData\Local\Programs\Python\python38\Lib\site-packages\requests_html.py",line 727,in browser
self.loop = asyncio.get_event_loop()
File "C:\Users\intel\AppData\Local\Programs\Python\python38\Lib\asyncio\events.py",line 639,in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-3'.
以下是原始代码:
from flask import Flask
from requests_html import HTMLSession
from bs4 import BeautifulSoup
app = Flask(__name__)
@app.route('/<user>')
def hello_world(user):
session = HTMLSession()
r = session.get('https://medium.com/@' + str(user))
print(r)
r.html.render()
divs = r.html.find('div')
lst = []
for div in divs:
soup = BeautifulSoup(div.html,'html5lib')
div_tag = soup.find()
try:
title = div_tag.section.div.h1.a['href']
if title not in lst:
lst.append(title)
except:
pass
return "\n".join(lst)
if __name__ == '__main__':
app.run(debug=True)
解决方法
您应该使用构建在烧瓶之上的 Quart 库。这将解决您的问题。
安装:pip install quart
from quart import Quart
from requests_html import AsyncHTMLSession
app = Quart(__name__)
@app.route('/<user>')
def hello_world(user):
session = AsyncHTMLSession()
r = await session.get('https://medium.com/@' + str(user))
print(r)
await r.html.arender()
divs = r.html.find('div')
lst = []
for div in divs:
soup = BeautifulSoup(div.html,'html5lib')
div_tag = soup.find()
try:
title = div_tag.section.div.h1.a['href']
if title not in lst:
lst.append(title)
except:
pass
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。