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
and that's the error
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 .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" ) 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 |
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