Advertisement
Try95th

getDECstars [Glassdoor reviews] for q_74587603

Dec 2nd, 2022 (edited)
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. ## for https://stackoverflow.com/q/74587603/6146136
  2.  
  3. ## SHORTER VERSION ##
  4. def getDECstars(starCont, mSoup, outOf=5):
  5.     try:
  6.         demc = [c for c in starCont.get('class', []) if c[:4]=='css-'][0].replace('css-', '', 1)
  7.         cssStyle = mSoup.select_one(f'style[data-emotion-css="{demc}"]')
  8.         if not cssStyle: cssStyle = mSoup.select_one(f'style:-soup-contains(".css-{demc}")') ## edit
  9.         cssStyle = cssStyle.get_text().replace('90deg, #0caa41 ', '90deg,#0caa41 ') ## edit
  10.         rPerc = float(cssStyle.split('90deg,#0caa41 ', 1)[1].split('%')[0])
  11.         return float(f'{(rPerc/100)*outOf if type(outOf) == int and outOf > 0 else rPerc:.3}')
  12.     except: return None
  13.  
  14. ## WITH ERROR BREAKDOWN ##
  15. def getDECstars(starCont, mSoup, outOf=5,  isv=False):
  16.     classList = starCont.get('class', [])
  17.     if type(classList) != list: classList = [classList]
  18.     classList = [str(c) for c in classList if str(c).startswith('css-')]
  19.     if not classList:
  20.         if isv: print('Stars container has no "css-" class')
  21.         return None
  22.    
  23.     demc = classList[0].replace('css-', '', 1)
  24.     demc_sel = f'style[data-emotion-css="{demc}"]'
  25.     cssStyle = soup.select_one(demc_sel)
  26.     if not cssStyle: ## edit
  27.         cssStyle = soup.select_one(f'style:-soup-contains(".css-{demc}")') ## edit
  28.         if isv: print('with -soup-contains:', cssStyle) ## edit
  29.     if not cssStyle:
  30.         if isv: print(f'Nothing found with selector {demc_sel}')
  31.         return None
  32.    
  33.     cssStyle = cssStyle.get_text().replace('90deg, #0caa41 ', '90deg,#0caa41 ') ## edit
  34.     errMsg = ''
  35.     if '90deg,#0caa41 ' not in cssStyle: errMsg += 'No #0caa41'
  36.     if '%' not in cssStyle.split('90deg,#0caa41 ', 1)[-1][:20]:
  37.         errMsg += ' No %'
  38.     if not errMsg:
  39.         rPerc = cssStyle.split('90deg,#0caa41 ', 1)[-1]
  40.         rPerc = rPerc.split('%')[0]
  41.         try:  
  42.             rPerc = float(rPerc)
  43.             if 0 <= rPerc <= 100:
  44.                 if type(outOf) == int and outOf > 0: rPerc = (rPerc/100)*outOf
  45.                 return float(f'{float(rPerc):.3}')
  46.             errMsg = f'{demc_sel} --> "{rPerc}" is out of range'
  47.         except: errMsg = f'{demc_sel} --> cannot convert to float "{rPerc}"'
  48.     if isv: print(f'{demc_sel} --> unexpected format {errMsg}')
  49.     return None
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement