Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## for https://stackoverflow.com/q/74587603/6146136
- ## SHORTER VERSION ##
- def getDECstars(starCont, mSoup, outOf=5):
- try:
- demc = [c for c in starCont.get('class', []) if c[:4]=='css-'][0].replace('css-', '', 1)
- cssStyle = mSoup.select_one(f'style[data-emotion-css="{demc}"]')
- if not cssStyle: cssStyle = mSoup.select_one(f'style:-soup-contains(".css-{demc}")') ## edit
- cssStyle = cssStyle.get_text().replace('90deg, #0caa41 ', '90deg,#0caa41 ') ## edit
- rPerc = float(cssStyle.split('90deg,#0caa41 ', 1)[1].split('%')[0])
- return float(f'{(rPerc/100)*outOf if type(outOf) == int and outOf > 0 else rPerc:.3}')
- except: return None
- ## WITH ERROR BREAKDOWN ##
- def getDECstars(starCont, mSoup, outOf=5, isv=False):
- classList = starCont.get('class', [])
- if type(classList) != list: classList = [classList]
- classList = [str(c) for c in classList if str(c).startswith('css-')]
- if not classList:
- if isv: print('Stars container has no "css-" class')
- return None
- demc = classList[0].replace('css-', '', 1)
- demc_sel = f'style[data-emotion-css="{demc}"]'
- cssStyle = soup.select_one(demc_sel)
- if not cssStyle: ## edit
- cssStyle = soup.select_one(f'style:-soup-contains(".css-{demc}")') ## edit
- if isv: print('with -soup-contains:', cssStyle) ## edit
- if not cssStyle:
- if isv: print(f'Nothing found with selector {demc_sel}')
- return None
- cssStyle = cssStyle.get_text().replace('90deg, #0caa41 ', '90deg,#0caa41 ') ## edit
- errMsg = ''
- if '90deg,#0caa41 ' not in cssStyle: errMsg += 'No #0caa41'
- if '%' not in cssStyle.split('90deg,#0caa41 ', 1)[-1][:20]:
- errMsg += ' No %'
- if not errMsg:
- rPerc = cssStyle.split('90deg,#0caa41 ', 1)[-1]
- rPerc = rPerc.split('%')[0]
- try:
- rPerc = float(rPerc)
- if 0 <= rPerc <= 100:
- if type(outOf) == int and outOf > 0: rPerc = (rPerc/100)*outOf
- return float(f'{float(rPerc):.3}')
- errMsg = f'{demc_sel} --> "{rPerc}" is out of range'
- except: errMsg = f'{demc_sel} --> cannot convert to float "{rPerc}"'
- if isv: print(f'{demc_sel} --> unexpected format {errMsg}')
- return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement