20
loading...
This website collects cookies to deliver better user experience
bs4
.import requests
from bs4 import BeautifulSoup
200
, we use BeautifulSoup
to parse it.url = 'https://sprott.com/investment-strategies/physical-commodity-funds/uranium/'
r = requests.get(url)
if r.status_code == 200:
soup: BeautifulSoup = BeautifulSoup(r.content, "html.parser")
prettify()
function to see it in your Jupyter Notebook with print(soup.prettify()
.<div class="cell small-6 large-3 fundHeader_data">
<h3 class="fundHeader_title">
Premium/Discount
</h3>
<div class="fundHeader_value">
-2.55%
</div>
<!-- <div class="fundHeader_detail">52 wk: <strong>$31.45 - $45.98</strong></div>-->
</div>
<div class="cell small-6 large-3 fundHeader_data">
<h3 class="fundHeader_title mt05">
Total lbs of U
<sub>
3
</sub>
O
<sub>
8
</sub>
</h3>
<div class="fundHeader_value">
40,780,707
</div>
div class
called "fundHeader_value." To get all of them and extract the ones with the share price and Uranium stocks, we use BeautifulSoup
findall
function storing it in a variable called fund_values
(a list).fund_values = soup.find_all('div', class_='fundHeader_value')
shareprice = fund_values[4].contents
['\r\n $US11.81\r\n ', <span class="fundHeader_icon fundHeader_icon--pos"><i data-feather="trending-up"></i></span>, '\n']
shareprice[0]
. We then want to get rid of the other stuff around it, namely white spaces and key returns. To make sure we're manipulating a string object, we can tell Python to recognize it as a string with str(shareprice[0])
. Python has a very powerful method for "stripping" away whitespace with .strip()
, so we call that after our string str(shareprice[0]).strip()
..replace('$US','')
on it and it returns 11.81.shareprice_value = str(shareprice[0]).strip().replace('$US','')
fund_values
.u3o8 = fund_values[6].contents
u3o8_stock = str(u3o8[0]).strip().replace(',','')
import requests
from bs4 import BeautifulSoup
url = 'https://sprott.com/investment-strategies/physical-commodity-funds/uranium/'
r = requests.get(url)
if r.status_code == 200:
soup: BeautifulSoup = BeautifulSoup(r.content, "html.parser")
fund_values = soup.find_all('div', class_='fundHeader_value')
shareprice = fund_values[4].contents
shareprice_value = str(shareprice[0]).strip().replace('$US','')
u3o8 = fund_values[6].contents
u3o8_stock = str(u3o8[0]).strip().replace(',','')