Python + Beautifulsoup: How to Get 'Href' Attribute of 'A' Element

Python Beautifulsoup, get href tag, in a tag

I believe that your problem lies in this line :

product_link = title.get('a')['href']

You already have a list of "a" elements, so you probably just need :

product_link = title['href']

Extract 'href' from tag using beautiful soup

After getting the span tag you need to find the a tag and then grab the href attribute.

Something like this would work:

for name in soup.find_all("span", {"class": "small"}):
print(name.find("a").get("href"))

Get href Attribute Link from td tag BeautifulSoup Python

This works for me in Python 2.7:

table = soup.find('table', {'class': 'tableFile2'})
rows = table.findAll('tr')
for tr in rows:
cols = tr.findAll('td')
if len(cols) >= 4 and "2013" in cols[3].text:
link = cols[1].find('a').get('href')
print link

A few issues with your previous code:

  1. soup.find() requires a dictionary of attributes (e.g., {'class' : 'tableFile2'})
  2. Not every cols instance will have at least 3 columns, so you need to check length first.

how to extract href attribute of ‘a’ element using id= instead of class name

Use this:

my_data = soupeddata.find_all('a', attrs = {'id': 'author-text'})

You can also pass class attribute inside the dict.

From the BeautifulSoup documentation:

Some attributes, like the data-* attributes in HTML 5, have names that
can’t be used as the names of keyword arguments:

data_soup = BeautifulSoup('<div data-foo="value">foo!</div>') 
data_soup.find_all(data-foo="value")
# SyntaxError: keyword can't be an expression

You can use these attributes in searches by putting them
into a dictionary and passing the dictionary into find_all() as
the attrs argument:

data_soup.find_all(attrs={"data-foo": "value"}) 
# [<div data-foo="value">foo!</div>]

How to get href attribute from given code in beautiful soap

Since the div & a tag are next to each otherdiv.a was possible.

link = [div.a['href'] for div in 
soup.find_all('div', attrs={'class' : 'yuRUbf'})]


Related Topics



Leave a reply



Submit