Forums

[Errno 111] Connection refused

Exception on / [GET] Traceback (most recent call last): File (First Line of Error) sock.connect(sa) ConnectionRefusedError: [Errno 111] Connection refused(Last Line of Error)

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()

It simply says :Internal Error either the application has a fault or the server is overloaded. This is a joke site..so, I don't think it is blocking requests or anything.The app shows no errors in the console, just the usual running at 127.0.0.1 Any ideas?

looks like this one is going to remain unaswered. :(

Better to use headless Chrome. We enabled features on your account that allows that.

Oh! Thanks. I thought only firefox was supported.

Oh! Thanks. I thought only firefox was supported. Will give that a try.

Sorry, same error.

The site you try to scrape could be blocking you.

You think? It seems unlikely. Well, I cannot think of any other reason. So maybe that's it.

It worked after I started to catch the Exceptions. It was not able to load the template because I had given the absolute path. The right way is to create a folder called 'templates' and then simply give the file name, no path. Can't run my website though because it is a free account and it requires the site I am pulling data from white listed and API access...too much hassle. I'm just glad, I got it to work. Thanks for your support.

from selenium import webdriver
from flask import Flask, render_template
import random
from pyvirtualdisplay import Display


app = Flask(__name__)
with Display():
     options = webdriver.ChromeOptions()
     options.add_argument('--headless')
     driver = webdriver.Chrome(chrome_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('joke.html', s=mylist)

    if __name__ == "__main__":
        app.run(debug=True)
except Exception as e:
    print(e)

Well, glad to hear you got it to work eventually!