Home > Blockchain >  How can I remove empty space in float from a number I get from web scraping? Error: could not conver
How can I remove empty space in float from a number I get from web scraping? Error: could not conver

Time:04-02

in the code I try to take price data from website. This website use an empty space in the price and the float class raises a flag:could not convert string to float: '1\xa0364' this code should extract the price from the website however the empty space in the price from the website information causes an error. I am not sure if the code works or not but it does not go further to study other function.

This is actually the price: 1364, but it gives: 1\xa0364'

Please see the code:


URL = 'https://www.reebok.se/zig-kinetica-ii-edge-gore-tex/H05172.html'
headers={"user-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0'}
def check_price():
    page = requests.get(URL , headers=headers)
    soup = BeautifulSoup(page.content, 'html.parser')
    
    title = soup.find( class_  = 'gl-heading gl-heading--regular gl-heading--italic name___1EbZs').get_text()
    print(title)
    price=soup.find( class_ ='gl-price-item gl-price-item--sale notranslate').get_text()
    converted_price= float(price[0:5])
        

CodePudding user response:

you can use replace for this kind of things, your code should be somting like this:

price_str = "1\xa0364"
price_str = sprice_str.replace(u'\xa0', u' ') # u is because of unicode char \xa0
price = float(price_str)

CodePudding user response:

you can use browse aı for simple web scraping.

  • Related