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
74 views

1 answer to this question.

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 130 views
0 votes
1 answer
0 votes
1 answer

How to get the multiplication table of any number using list comprehension?

Hey @Neha, you can use something like ...READ MORE

answered Jul 8 in Python by Sakshi
22 views
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
58 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
16 views
0 votes
1 answer