Good Day:)

I'm trying to figure out how to remove the % sign that is returned from scraping Yahoo Finance without removing the decimal point (or a possible negative sign in front of number returned as netProfit). My goal is to convert the string without the % sign a float that I can use later. netProfit1 is close to what I'm after but removed the -ve sign on other examples and also the decimal point

All help appreciated
using python 3.4 BTW - Still learning :)

This is what it currently returns

C:\Python34\python.exe C:/_Python/test.py
Net Profit Margin (The higher the better):  8.01%
Net Profit Margin (The higher the better):  801
Net Profit Margin (The higher the better):  5.67%
Net Profit Margin (The higher the better):  567

Process finished with exit code 0

Here is the code

import time
import urllib.request
import urllib.error
import urllib.parse
import bs4
import requests
from bs4 import BeautifulSoup as soup
from bs4 import BeautifulSoup
from urllib.request import urlopen
import sys
import math
from math import sqrt
import decimal
import string

tickerTST = ['WEF.TO','CUS.TO']

def yahooKeyStats(stock):
    try:
        optionsUrl = "https://ca.finance.yahoo.com/q/ks?s=" + stock
        optionsPage = urlopen(optionsUrl)
        soup = BeautifulSoup(optionsPage)


# 12    Operating Margin (ttm):8.01% (AKA Net Profit Margin)


        tag12 = soup.findAll('td', {'class': 'yfnc_tablehead1'})[12]

        tag12 = soup.findAll('td', {'class': 'yfnc_tablehead1'})[12]

        netProfit = str(tag12.nextSibling.text) # this is what I am currently getting I'd like to remove only the % sign
        print(('Net Profit Margin (The higher the better):') + ('\t') + str(netProfit))

        netProfit1 = str().join([i for i in tag12.nextSibling.text if i.isdigit()]) # this works but removes the decimal as well
        print(('Net Profit Margin (The higher the better):') + ('\t') + str(netProfit1))


    except Exception as e:
        sys.stdout.write("nope")



################### TICKER LOOP #################
for eachStock in tickerTST:  # for every entity in the above this run yahookey stats
    yahooKeyStats(eachStock)
    time.sleep(2)

Try this:

re.findall('[\d.]+','2123.32321%')

outputs:
'2123.32321'

re is regular expressions module so you have to import re

Edited 1 Year Ago by Slavi

This question has already been answered. Start a new discussion instead.