【发布时间】:2026-02-20 19:50:01
【问题描述】:
from selenium import webdriver
from flask import Flask, render_template
import random
from pyvirtualdisplay import Display
from selenium.webdriver.firefox.options import Options as FirefoxOptions
app = Flask(__name__)
with Display():
options = FirefoxOptions()
options.add_argument('--headless')
driver = webdriver.Firefox(firefox_options=options)
driver.implicitly_wait(10)
t_file = '/home/parshuram/mysite/static/links'
target = open(t_file).read().splitlines()
try:
@app.route('/')
def get_jokes():
driver.get(random.choice(target))
jokes = driver.find_elements_by_tag_name('p')
mylist = []
for joke in jokes:
mylist.append(joke.text)
return render_template('/home/parshuram/mysite/Template/joke.html', s=mylist)
if __name__ == "__main__":
app.run()
finally:
driver.quit()
2020-10-05 17:59:33,327:异常 / [GET] Traceback(最近 最后调用):文件 “/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py”, 第 2446 行,在 wsgi_app 中 响应 = self.full_dispatch_request() 文件“/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py”, 第 1951 行,在 full_dispatch_request 中 rv = self.handle_user_exception(e) 文件“/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py”, 第 1820 行,在 handle_user_exception 中 reraise(exc_type,exc_value,tb)文件“/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/_compat.py”, 第 39 行,在再加注中 提高价值文件“/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py”, 第 1949 行,在 full_dispatch_request 中 rv = self.dispatch_request() 文件“/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py”, 第 1935 行,在 dispatch_request 中 在 get_jokes 中返回 self.view_functionsrule.endpoint 文件“/home/parshuram/mysite/sick.py”,第 18 行 driver.get(random.choice(target)) 文件“/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py”, 第 248 行,在获取 self.execute(Command.GET, {'url': url}) 文件“/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py”, 第 234 行,执行中 response = self.command_executor.execute(driver_command, params) 文件 “/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py”, 第 401 行,在执行中 return self._request(command_info[0], url, body=data) 文件“/home/parshuram/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py”, 第 432 行,在 _request 中 self._conn.request(method, parsed_url.path, body, headers) 文件“/usr/lib/python3.8/http/client.py”,第 1230 行,在请求中 self._send_request(method, url, body, headers, encode_chunked) 文件“/usr/lib/python3.8/http/client.py”,第 1276 行,在 _send_request self.endheaders(body, encode_chunked=encode_chunked) 文件“/usr/lib/python3.8/http/client.py”,第 1225 行,在 endheaders self._send_output(message_body, encode_chunked=encode_chunked) 文件“/usr/lib/python3.8/http/client.py”,第 1004 行,在 _send_output self.send(msg) 文件“/usr/lib/python3.8/http/client.py”,第 944 行,在发送中 self.connect() 文件“/usr/lib/python3.8/http/client.py”,第 915 行,在连接中 self.sock = self._create_connection(文件“/usr/lib/python3.8/socket.py”,第 807 行,在 create_connection 在 create_connection 中引发错误文件“/usr/lib/python3.8/socket.py”,第 796 行 sock.connect(sa) ConnectionRefusedError: [Errno 111] 连接被拒绝
它表示应用程序有错误或服务器过载。现在,这只是从一个笑话网站中提取一些文本。因此,该站点不太可能拒绝连接。有什么想法吗?
【问题讨论】:
标签: python python-3.x selenium flask pythonanywhere