Use a function arg in soup
Sayth Renshaw
flebber.crue at gmail.com
Wed Aug 1 07:30:51 EDT 2018
Hi.
I want to use a function argument as an argument to a bs4 search for attributes.
I had this working not as a function
# noms = soup.findAll('nomination')
# nom_attrs = []
# for attr in soup.nomination.attrs:
# nom_attrs.append(attr)
But as I wanted to keep finding other elements I thought I would turn it into a generator function.
def attrKey(element):
for attr in soup.element.attrs:
yield attr
results in a Nonetype as soup.element.attrs is passed and the attribute isn't substituted.
# Attempt 2
def attrKey(element):
for attr in "soup.{}.attrs".format(element):
yield attr
# Attempt 3
def attrKey(element):
search_attr = "soup.{}.attrs".format(element)
for attr in search_attr:
yield attr
so I would call it like
attrKey('nomination')
Any ideas on how the function argument can be used as the search attribute?
Thanks
Sayth
More information about the Python-list
mailing list