jamjamjam jamjamjam - 1 year ago 104
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"})

The result is:

[<div class="cena">


</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 Source

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(",", ""))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download