Griboedov
Griboedov

Reputation: 433

BeautifulSoup get text links?

I have the following code:

soup = BeautifulSoup(content, "html.parser")
block = soup.select('.meta-info a')

I need to get all links in block .meta_info

After I try to get all text of links from array block, only that have email address as text link.

I mean:

<a href="">Bla bla [email protected]</a>

How can I get it?

I tried as:

 for item in block:
            email_par = emailFromString(item.text)

            if email_par[0]:
                pass

But it works not stable

Upvotes: 0

Views: 210

Answers (2)

宏杰李
宏杰李

Reputation: 12158

import re
soup.find_all(name='a', text=re.compile(r'@'))

Upvotes: 1

roy
roy

Reputation: 97

this link might be helpful css select with regex

if you still cant figure it out try this, make sure you have the latest version of BeautifulSoup

import re
soup = BeautifulSoup(content, "html.parser")
block = soup.select('.meta-info a')

emails = block.find_all(text=re.compile('.*@.*\.com'))

now you can iterate over it and extract the text from each of the links

Upvotes: 1

Related Questions