0

I'm new in python and flask, and I'm building a web scraper app using Python3 and Flask. But I have a problem with this error: "UnboundLocalError: local variable 'price' referenced before assignment". I don't know what I did wrong in the program to get this error .

I tried 'global a' with initial variable 'a = 0' and wrote the code, but it displays the initial variable only '0' without any change even I changed the URL.

Here is app.py:

# Import Packages
from flask import Flask, render_template, request, url_for
from bs4 import BeautifulSoup
import requests

app = Flask(__name__)

price = 0      # Initial variable or default variable for global variable

@app.route("/", methods=['GET', 'POST'])
def HomePage():
    url = request.form.get('asin')

    try:
        res = requests.get(url)
        soup = BeautifulSoup(res.text, 'html.parser')

        # Here is I updated it to set the 'price' variable to be global 
        global price
        print(price)

        # Check the price location on the page
        price = soup.select_one("#priceblock_ourprice").text

    except:
        pass

    return render_template('home.html', url=url, price=price)     # I write the 'price' variable in 'render_template' to display it in the html file

if __name__ == "__main__":
    app.run(debug=True)

Here is home.html:


<body>
    <form action="#" method="POST">
        <input type="url" name="asin" id="asin" placeholder="enter the url here" value="{{ request.form.asin }}">

    </form>
    {% if request.form.asin %}
        url: {{ url }} <br>
        price: {{ price }} <!-- it displays the default price variable which is 0 whithout changing it value even with changing the url -->
    {% endif %}

</body>

Now with this code it displays the price = 0 without changing the value even though I changed the URL, and what I need is to display the price of the product URL.

How do I solve the problem?

Emma
  • 27,428
  • 11
  • 44
  • 69
Yehia Fouad
  • 21
  • 1
  • 3
  • 1
    Consider what happens if an exception is raised before you can execute `global price`. – chepner Jul 28 '19 at 20:53
  • It gives me this error "UnboundLocalError: local variable 'price' referenced before assignment " that's why I put global price, but it displays the value 0 only without changing – Yehia Fouad Jul 28 '19 at 21:06

1 Answers1

0
  1. It's a bad idea to use a global variable in-order to send value to a function, it should accept a default parameter func_name(default_price = value).
  2. There is no need to use global before printing it, as the variable named 'price' is in the containing scope, the 'global' keyword is required when you want to change a variable from the outer scope, i.e: when you do price = soup.select_one("#priceblock_ourprice").text and again, it is highly discouraged. See following post: Use of "global" keyword in Python
  3. My GUESS is that, when you are runing your code using app.run (and i am unsure about it), it is created in a different scope than the one you are expecting to run at (which is the scope of the module you have given). If i were you, i would add a break point before the statement 'global price' and see if the variable price is shown. If not, then the framework is runing your code in a different scope which doesn't recognize your global variables (either way, you should pass it as parameter to the function).
Guy Tabak
  • 128
  • 7
  • yes this is what happened with me and when I remove the global variable , it shows this error " UnboundLocalError: local variable 'price' referenced before assignment " but how can I display the value of price without global variable ? – Yehia Fouad Jul 28 '19 at 21:49