Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 03-31-2023, 08:30 PM   #1
dunhill
Guru
dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.
 
dunhill's Avatar
 
Posts: 724
Karma: 228142
Join Date: Sep 2017
Location: 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
And I have changed it to this:

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.
dunhill is offline   Reply With Quote
Old 04-01-2023, 09:15 AM   #2
thiago.eec
Guru
thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.
 
Posts: 930
Karma: 1177583
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
P.S.: ISBN here is assumed to be a string, so I didn't change this.
thiago.eec is offline   Reply With Quote
Advert
Old 04-01-2023, 04:20 PM   #3
dunhill
Guru
dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.
 
dunhill's Avatar
 
Posts: 724
Karma: 228142
Join Date: Sep 2017
Location: Argentina
Device: moon+ reader, kindle paperwhite
Quote:
Originally Posted by thiago.eec View Post
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
P.S.: ISBN here is assumed to be a string, so I didn't change this.

thiago.eec
Thanks for the code.
Works !!

Last edited by dunhill; 04-01-2023 at 04:49 PM.
dunhill is offline   Reply With Quote
Old 04-01-2023, 05:46 PM   #4
thiago.eec
Guru
thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.
 
Posts: 930
Karma: 1177583
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite
Quote:
Originally Posted by dunhill View Post
thiago.eec
Thanks for the code.
Works !!
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}"
thiago.eec is offline   Reply With Quote
Old 04-01-2023, 09:35 PM   #5
dunhill
Guru
dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.
 
dunhill's Avatar
 
Posts: 724
Karma: 228142
Join Date: Sep 2017
Location: Argentina
Device: moon+ reader, kindle paperwhite
Quote:
Originally Posted by thiago.eec View Post
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}"
Thank you the way you comment on this message is better.
I was using format for number this ${:,.0f}
dunhill is offline   Reply With Quote
Advert
Reply


Forum Jump

Similar Threads
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


All times are GMT -4. The time now is 02:46 AM.


MobileRead.com is a privately owned, operated and funded community.