johnrao07
johnrao07

Reputation: 6938

Find Href based on specific text in the name attribute of the a tag

<a class="cscore_link"  name="&lpos=house:schedule:final" href="https://www.url.com-2019-20">

There are multiple classes of cscore_link in the html, but the value of name attribute differs. I need to get the href value of all the cscore_link class where the value of name has a substring of final

Upvotes: 0

Views: 92

Answers (2)

KunduK
KunduK

Reputation: 33384

If you have Beautiful soup 4.7.1 or above you can use following css selector to find name contains final.

print(soup.select(".cscore_link[name*='final']"))

Or You can check name ends with final.

print(soup.select(".cscore_link[name$='final']"))

Upvotes: 1

Rakesh
Rakesh

Reputation: 82815

Use Regex.

Ex:

import re
from bs4 import BeautifulSoup

html = """<a class="cscore_link"  name="&lpos=house:schedule:final" href="https://www.url.com-2019-20"></a>
<a class="cscore_link"  name="&lpos=house:schedule" href="https://www.url.com-2019-20"></a>
"""

soup = BeautifulSoup(html, 'html.parser')
print(soup.find_all('a', {'class':'cscore_link', 'name': re.compile(r":final\b")})) 

Output:

[<a class="cscore_link" href="https://www.url.com-2019-20" name="&amp;lpos=house:schedule:final"></a>]

Upvotes: 1

Related Questions