![]() |
#1 |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 866
Karma: 409968
Join Date: Sep 2017
Location: Buenos Aires, Argentina
Device: moon+ reader, kindle paperwhite
|
Help with a code to get float data
I want to modify some data obtained by a plugin that they made for me and I don't want to be too upset with the programmer who has so kindly done it.
The original code includes the price in text format. With this parameter(pricespan.text) I am looking to get the data in decimal format ( float ) as seen in the code replacement but I don't get the result. If someone can give me a hand. Don't laugh at me if I replaced the wrong code This is the original code: Code:
def scrape (isbn): from bs4 import BeautifulSoup as bs import requests url = "https://www.tematika.com/catalogsearch/result/?q=" + isbn r = requests.get(url) soup = bs(r.text, 'html.parser') div = soup.find(id='jm-current-content') try: pricebox = div.find_all(class_="price-box") if len(price box) == 1: for p in pricebox: price range = p.find(class_="price") return pricespan.text the rest: return none except: return none Code:
def scrape (isbn): from bs4 import BeautifulSoup as bs import requests url = "https://www.tematika.com/catalogsearch/result/?q=" + isbn r = requests.get(url) soup = bs(r.text, 'html.parser') div = soup.find(id='jm-current-content') try: pricebox = div.find_all(class_="price-box") if len(price box) == 1: for p in pricebox: price range = p.find(class_="price") return float((pricespan.text).replace('.','').replace(',' , '.')) the rest: return none except: return none Last edited by dunhill; 04-01-2023 at 04:23 PM. |
![]() |
![]() |
![]() |
#2 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,188
Karma: 1318209
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite
|
Hi, @dunhill
Maybe I'm wrong, but I think you may have translated your original code to Spanish and then back to English. This caused some problem with python syntax getting changed, like 'try' to 'attempt'. Here is a working code, returning a 'float': Code:
def scrape (isbn): from bs4 import BeautifulSoup as bs import requests url = "https://www.tematika.com/catalogsearch/result/?q=" + isbn r = requests.get(url) soup = bs(r.text, 'html.parser') div = soup.find(id='jm-current-content') try: pricebox = div.find_all(class_="price-box") if len(pricebox) == 1: for p in pricebox: pricerange = p.find(class_="price") return float(pricerange.text.replace('$', '').replace('.','').replace(',' , '.')) else: return none except: return none |
![]() |
![]() |
![]() |
#3 | |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 866
Karma: 409968
Join Date: Sep 2017
Location: Buenos Aires, Argentina
Device: moon+ reader, kindle paperwhite
|
Quote:
thiago.eec Thanks for the code. Works !! Last edited by dunhill; 04-01-2023 at 04:49 PM. |
|
![]() |
![]() |
![]() |
#4 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,188
Karma: 1318209
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite
|
I could not answer at the time, but I saw your question before you deleted it.
In case you still want to know how to keep the '$' symbol and the '.' as a thousand separator, do this: 1) Preferences > Add your own column 2) Go to the desired column and double-click it 3) In 'Format for numbers', use this: "${:.10n}" |
![]() |
![]() |
![]() |
#5 | |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 866
Karma: 409968
Join Date: Sep 2017
Location: Buenos Aires, Argentina
Device: moon+ reader, kindle paperwhite
|
Quote:
I was using format for number this ${:,.0f} |
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Image won't float | gurugordon | Editor | 1 | 03-19-2020 05:54 PM |
Using float in ADR v1 and v2 | bobcdy | ePub | 5 | 01-31-2015 02:36 AM |
Float Question (ADE) | Tex2002ans | ePub | 19 | 08-02-2013 10:34 PM |
The operator >> is undefined for the argument type(s) float, float | twobob | Kindle Developer's Corner | 10 | 09-05-2012 12:50 PM |
Float bug in ADE ? | helenouchkaia | ePub | 16 | 01-08-2012 11:11 AM |