jamjamjam jamjamjam - 6 months ago 24
Python Question

How to extract numbers from the website

I've got a code:

import urllib.request
from bs4 import *
soup = BeautifulSoup(urllib.request.urlopen("http://biznes.pl/waluty/profile/fixing-nbp/jen-japonia,821,0,30,profile-waluta-nbp.html"), "lxml")
price = soup.find_all("div", {"class":"cena"})
print(price)


The result is:

[<div class="cena">

3,6178

</div>, <div class="cena Waluta">PLN</div>, <div class="cena up"></div>]


I want to separate number "3,6178" to use it in further calculations. How should I do it?

Answer

In this case, you can do the following:

p = price[0].string.strip()

to get the string "3,6178"

Note, however, that you will also want to get rid of the comma to do calculations:

p = int(p.replace(",", ""))