Advertisement
Try95th

selectForList [bs4]

Dec 2nd, 2022 (edited)
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. ##### select & get to extract a list of values/text from a bs4 Tag #####
  2. ## simplest version of confParse  [ https://pastebin.com/c0TjDmNE ] ####
  3.  
  4. ### examples of usage at                                          ######
  5. #####    solution1 of https://stackoverflow.com/a/74947523/6146136 OR ##
  6. #####    section#2 of https://stackoverflow.com/a/74655334/6146136 #####
  7.  
  8. ### if unfamiliar with CSS selectors, see                         ######
  9. #### https://beautiful-soup-4.readthedocs.io/en/latest/#css-selectors ##
  10. #####--> https://facelessuser.github.io/soupsieve/selectors/#selector ##
  11. #### https://www.w3schools.com/cssref/css_selectors.php ################
  12.  
  13.  
  14. ##################### EXTRACT A SINGLE VLAUE from a bs4 Tag #####################
  15. def selectGet(tagSoup, selector, targetAttr='', defaultVal=None):
  16.     sel, ta =  [str(p).strip() if p else '' for p in [selector, targetAttr]]
  17.     el, rVal = tagSoup.select_one(sel) if sel else tagSoup, defaultVal
  18.     if el: rVal = el.get(ta,defaultVal) if ta else el.get_text(' ', strip=True)
  19.     isClassList = ta=='class' and isinstance(rVal, list)
  20.     return ' '.join(f'{r}' for r in rVal) if isClassList else rVal
  21. #################################################################################
  22.  
  23.  
  24. def selectForList(tagSoup, selectors, printList=False):
  25.     if isinstance(selectors, dict):
  26.         return dict(zip(selectors.keys(), selectForList(
  27.             tagSoup, selectors.values(), printList)))
  28.    
  29.     selGen = (( list(sel if isinstance(sel, (tuple, list)) ## generate params
  30.                 else [sel])+[None]*2 )[:3] for sel in selectors)
  31.     returnList = [  sel[0] if sel[1] == '"staticVal"' ## [allows placeholders]
  32.                     else selectGet(tagSoup, *sel) for sel in selGen   ]
  33.    
  34.     if printList and not isinstance(printList,str): print(returnList)
  35.     if isinstance(printList,str): print(*returnList, sep=printList)
  36.     return returnList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement