Get link within html using BeautifulSoup

4k views Asked by At

I've this piece of code :

        <td colspan="2" class="fc_blabla">
        <a title="Blabla" href="http://www.blabla.com/.html">Blabla</a>
    </td>

I need to retrieve only the link, I tried many way like :

1#

for link in soup.find_all("td", { "class":"fc_blabla"}):
   url = link.find("href")
   print link

2#

print soup.select(".fc_blabla > href")

3#

for link in soup.find_all("a"):
   url = link.get("href")
   print url
2

There are 2 answers

3
Padraic Cunningham On BEST ANSWER
html="""
  <td colspan="2" class="fc_blabla">
        <a title="Blabla" href="http://www.blabla.com/.html">Blabla</a>
    </td>
    """

from bs4 import BeautifulSoup

soup = BeautifulSoup(html)

print(soup.find("td",attrs={"class":"fc_blabla"}).a["href"])

http://www.blabla.com/.html
7
Hooked On

You were looking for "href" in the "td" tag. It is in the "a" tag.

import bs4
soup = bs4.BeautifulSoup(raw_html)
td = soup.find('td', {"class":"fc_blabla"})
print td.find("a")["href"]