Zepol
Zepol

Reputation: 347

Python Beautiful Soup Syntax

I am looking for ways to simplify my beautiful soup code.

Normally while parsing I could do this:

content = soup.find_all('li')
links_from_content = content.find_all('a')

is there a way to do this single line? Something like:

content = harpatchnumber.find_all('li').find_all('a')

this seems to not be working so, I would like to know how i could do it better

Upvotes: 1

Views: 323

Answers (2)

debo
debo

Reputation: 380

You could do it in a single line with a list comprehension.

atags = [t.a for t in s.find_all('li') if t.a != None]

Upvotes: 0

宏杰李
宏杰李

Reputation: 12158

soup.select('li a')

this will return a list of a tag which is contained by li tag

Upvotes: 1

Related Questions