How to get anchor tags of particular class using BeautifulSoup?

0 votes

I am using BeatifulSoup for web scraping and I want to get the anchor tags of a particular class.

I was using this code to get all tags:

soup = BeautifulSoup(html)
for tag in soup.find_all('a'):
    print (tag.text)

But this gets all anchor tags. How to limit it to get links of only a particular class name?

Apr 2 in Python by Umesh
19 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can specify the class you want to get the anchor tags of. When you do this, only the data of that particular class name will be extracted. Example:

soup = BeautifulSoup(html)
for tag in soup.find_all('a',{"class":"example"}):
    print (tag.text)

answered Apr 2 by John

Related Questions In Python

0 votes
1 answer

How to print objects of class using print function in Python?

It can be done as shown below: class Element: ...READ MORE

answered Dec 3, 2018 in Python by Nymeria
• 3,500 points

edited Dec 19, 2018 by Nymeria 93 views
0 votes
1 answer
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,320 points
48 views
0 votes
1 answer

How to get text label from SAP using pywinauto[python]

Hi. Can you please tell me what ...READ MORE

answered Jun 28, 2018 in Python by Nietzsche's daemon
• 4,260 points
210 views
0 votes
1 answer

How to parse html file to BeautifulSoup?

Hey. Refer to the following code: driver.get("link") html = ...READ MORE

answered Apr 2 in Python by Kirti
12 views
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.