Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##### select & get to extract a list of values/text from a bs4 Tag #####
- ## simplest version of confParse [ https://pastebin.com/c0TjDmNE ] ####
- ### examples of usage at ######
- ##### solution1 of https://stackoverflow.com/a/74947523/6146136 OR ##
- ##### section#2 of https://stackoverflow.com/a/74655334/6146136 #####
- ### if unfamiliar with CSS selectors, see ######
- #### https://beautiful-soup-4.readthedocs.io/en/latest/#css-selectors ##
- #####--> https://facelessuser.github.io/soupsieve/selectors/#selector ##
- #### https://www.w3schools.com/cssref/css_selectors.php ################
- ##################### EXTRACT A SINGLE VLAUE from a bs4 Tag #####################
- def selectGet(tagSoup, selector, targetAttr='', defaultVal=None):
- sel, ta = [str(p).strip() if p else '' for p in [selector, targetAttr]]
- el, rVal = tagSoup.select_one(sel) if sel else tagSoup, defaultVal
- if el: rVal = el.get(ta,defaultVal) if ta else el.get_text(' ', strip=True)
- isClassList = ta=='class' and isinstance(rVal, list)
- return ' '.join(f'{r}' for r in rVal) if isClassList else rVal
- #################################################################################
- def selectForList(tagSoup, selectors, printList=False):
- if isinstance(selectors, dict):
- return dict(zip(selectors.keys(), selectForList(
- tagSoup, selectors.values(), printList)))
- selGen = (( list(sel if isinstance(sel, (tuple, list)) ## generate params
- else [sel])+[None]*2 )[:3] for sel in selectors)
- returnList = [ sel[0] if sel[1] == '"staticVal"' ## [allows placeholders]
- else selectGet(tagSoup, *sel) for sel in selGen ]
- if printList and not isinstance(printList,str): print(returnList)
- if isinstance(printList,str): print(*returnList, sep=printList)
- return returnList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement