Anatol Zakrividoroga
Anatol Zakrividoroga

Reputation: 4518

How to extract link under a <li> tag with a specific class?

<li class="a-last"><a href="/macbook-pro">Buy Now</a></li>

How can you extract the link /macbook-pro inside the class a-last? Efficiency is a consideration.

Upvotes: 0

Views: 745

Answers (3)

Stanislav Lipovenko
Stanislav Lipovenko

Reputation: 574

This is the list of all needed hrefs:

[el.find('a').get('href') for el in soup.find_all('li', {'class': 'a-last'})]

Upvotes: 0

Andrej Kesely
Andrej Kesely

Reputation: 195438

One possibility is CSS selectors:

data = '''<li class="a-last"><a href="/macbook-pro">Buy Now</a></li>'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'lxml')

print(soup.select_one('li.a-last [href]')['href'])

Prints:

/macbook-pro

li.a-last [href] will select tag with attribute href that is under <li> tag with class a-last.

If you want to be more specific and want to extract only <a> tag directly under <li class="a-last">, you can use:

print(soup.select_one('li.a-last > a[href]')['href'])

Upvotes: 1

Maaz
Maaz

Reputation: 2445

You can do this:

from bs4 import BeautifulSoup

html = """<li class="a-last"><a href="/macbook-pro">Buy Now</a></li>"""
soup = BeautifulSoup(html, 'html.parser')

href = soup.find('li', {'class': 'a-last'}).find('a').get('href')

print(href)

RESULTS:

/macbook-pro

Upvotes: 1

Related Questions