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:
soup.find()
requires a dictionary of attributes (e.g.,{'class' : 'tableFile2'}
)- 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
How to Run Python Script from Another Machine Without Installing Imported Modules
How to Save All the Variables in the Current Python Session
How to Check Whether a Number Is Divisible by Another Number
How to Run Linux Terminal Command in Python in New Terminal
How to Get the Sum of a CSV Column List to Print
Passing Multiple Arguments from Django Template Href Link to View
How to Stop a Running Function Without Exiting the Tkinter Window Entirely
How-To Run Tensorflow on Multiple Core and Threads
How to Extract Column Value Within Square Brackets in Pyspark
How Best to Insert Nan Values in a Python List by Referring to an Already Sorted List
No Output Displays When Execute Python File
Convert Number Strings With Commas in Pandas Dataframe to Float
How to Convert Python Code to Application
How to Convert Data from Txt Files to Excel Files Using Python
Making a Matrix in Python 3 Without Numpy Using Inputs
How to Ask a Set of Questions Multiple Times Based on User Input
Python Handling Socket.Error: [Errno 104] Connection Reset by Peer