Python method chaining in a for loop

70 Views Asked by At

I have a list of html tags defined as below:

tags = ["text1", "text2", "text3"]

I will need to use theses values in a method chaining as shown below:

soup.find("div", class_="text1").find("div", class_="text2").find("div", class_="text3").text

Since the list is dynamic, is there any way i can make the method chaining find() dynamic as well (in a for loop maybe?)

4

There are 4 best solutions below

1
ForceBru On BEST ANSWER
def find_chained(soup, classes):
    for cls in classes:
        soup = soup.find("div", class_=cls)
    return soup.text

Should be self-explanatory: loop over each item; the tag you found is now the new "base" tag within which you want to find the next div.

0
Ukulele On

Yes, a for loop can be used. Here each iteration we replace relevant_section with its first child of the right class:

tags = ["text1", "text2", "text3"]
relevant_section = soup
for tag in tags:
    relevant_section = relevant_section.find("div", class_=tag)
relevant_text = relevant_section.text

1
diggusbickus On

just use select

css_class = ' '.join([f'.{cls}' for cls in tags])
soup.select(css_class).text
0
chepner On

You can use functools.reduce to automate the process of replacing soup with the result of a call to soup.find:

from functools import reduce


classes = ["text1", "text2", "text3"]

result = reduce(lambda s, x: s.find("div", class_=x), classes, soup).text

or, if you like functional tomfoolery more than the lambda expression :),

from operator import call, methodcaller

result = reduce(call,
                [methodcaller("find", "div", class_=x) for x in classes],
                soup).text