Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
local variable troubles
#1
Hi,i was following a tutorial and typing my own code with jupyter notebook. and my problem is the code that i wrote has an error and the original(from the tutorial) code work well.
i checked and re-checked but i can't find any difference between the 2 code. can you help me to find the error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from bs4 import BeautifulSoup
 
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
 
import re
import time
 
 
class AmazonBot(object):
    global price
     
    def __init__(self,items):
        self.amazon_url = "https://www.amazon.ca/"
        self.items = items
         
        self.profile = webdriver.FirefoxProfile()
        self.options = Options()
        self.driver = webdriver.Firefox()
        ##self.driver = webdriver.Firefox(firefox_profile=self.profile, firefox_options=self.options)
         
        self.driver.get(self.amazon_url)
         
    def search_items(self)     :
        urls=[]
        prices=[]
        names=[]
         
        for item in self.items:
            print(f"Searching for {item}")
             
            self.driver.get(self.amazon_url)
             
            search_input = self.driver.find_element_by_id("twotabsearchtextbox")
            search_input.send_keys(item)
             
            time.sleep(2)
              
            search_button = self.driver.find_element_by_xpath('//*[@id="nav-search"]/form/div[2]/div/input')  
            search_button.click()
             
            time.sleep(2)
             
            first_result = self.driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[1]/div[2]/div/span[3]/div[1]/div[1]")
            asin = first_result.get_attribute("data-asin")
            url = "https://www.amazon.ca/dp/" + asin
            price = self.get_product_price(url)
            name = self.get_product_name(url)
             
             
             
            prices.append(price)
            urls.append(url)
            names.append(name)
             
             
            print(price)
            print(name)
            print(url)
             
            time.sleep(2)
             
             
        return prices,urls,names    
 
 
    def get_product_price(self,url):
        self.driver.get(url)
          
         
        try:
            price = self.driver.get_element_by_id("priceblock_ourprice").text
         
        except:   
            pass
     
        try:
            price = self.driver.get_element_by_id("priceblock_dealprice").text
         
        except:   
            pass
     
        if price is None:
            price = "Note available"
        else:
            non_decimal = re.compile(r'[^\d.]+')
            price = non_decimal.sub('',price)
         
        return price   
     
     
     
     
    def get_product_name(self,url):
        self.driver.get(url)
        try:
            product_name= self.driver.get_element_by_id("productTitle")
         
        except
            pass
         
        if product_name is None:
                product_name = "Note available"
             
        return product_name
     
and that's the error
Error:
--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) <ipython-input-15-dc98a460d1f2> in <module> 2 amazon_bot = AmazonBot(items) 3 ----> 4 amazon_bot.search_items() <ipython-input-14-f083c7822166> in search_items(self) 50 asin = first_result.get_attribute("data-asin") 51 url = "https://www.amazon.ca/dp/" + asin ---> 52 price = self.get_product_price(url) 53 name = self.get_product_name(url) 54 <ipython-input-14-f083c7822166> in get_product_price(self, url) 86 pass 87 ---> 88 if price is None: 89 price = "Note available" 90 else: UnboundLocalError: local variable 'price' referenced before assignment
the original code is here the code link
Reply
#2
There is two chances to define price as they are contained in two try/except blocks that pass on any exception.
1
2
3
4
5
6
7
8
9
10
11
try:
    price = self.driver.get_element_by_id("priceblock_ourprice").text
          
except:   
    pass
      
try:
    price = self.driver.get_element_by_id("priceblock_dealprice").text
          
except:   
    pass
if both of these raise an exception price wont be defined.
Reply
#3
I would make the second pass in those try except blocks price = None.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
but i wonder why in the original code (in link), it work perfectly ????
Reply
#5
It's probably getting different data that didn't expose the problem Yoriz pointed out. You've changed quite a bit of the code. So you are getting different data, and your data doesn't have the elements that the try/except blocks are looking for.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  creating arbitrary local variable names Skaperen 9 1,898 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  how solve: local variable referenced before assignment ? trix 5 1,827 Jun-15-2024, 07:15 PM
Last Post: trix
  troubles with tqdm max22 2 1,587 Nov-27-2023, 09:20 PM
Last Post: max22
  It's saying my global variable is a local variable Radical 5 5,996 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 3,041 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  Installation troubles on Win 10 peringek 1 3,678 Dec-31-2020, 07:30 AM
Last Post: caleb_cruze
  converting user input to float troubles RecklessTechGuy 3 3,672 Aug-17-2020, 12:41 PM
Last Post: deanhystad
  csv troubles DPaul 4 3,441 Aug-13-2020, 05:18 PM
Last Post: DPaul
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 3,123 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  local variable 'marks' referenced before assignment Calli 3 3,153 May-25-2020, 03:15 PM
Last Post: Calli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020