2

I am writing a Python 3 code for web scraping using beautiful soup. Below is the code:

from bs4 import BeautifulSoup as BS


soup = BS(html, 'html.parser')

# finding all 'a' tags of the page whose class was 'image'
containers = soup.findAll('a', {'class': 'image'})

print(len(containers))  # containers length is 15

Now I need to write a loop and find out all the href components from the 'a' tag. How do I do this ?

Rob
  • 3,333
  • 5
  • 28
  • 71
Devashish Nigam
  • 101
  • 1
  • 1
  • 4
  • 4
    Sorry, we [can't accept images of code, errors or output](https://meta.stackoverflow.com/a/285557). Post those as *text*, so that we can try to reproduce the problem without having to re-type everything, and your question can be properly indexed or read by screen readers. – Martijn Pieters Feb 09 '19 at 19:31
  • See the duplicate, just use `for link in containers:` then `link['href']`. You really want to use `page_soup.select('a.image[href]')` instead; that selects the same `a` tags with the `image` class, but adds an additional requirement that those tags are guaranteed to have a `href` attribute. `.select()` lets you use CSS selectors to find matches. – Martijn Pieters Feb 09 '19 at 19:34
  • Take a look at a similar question asked [here](https://stackoverflow.com/questions/2612548/extracting-an-attribute-value-with-beautifulsoup) – cwahls Feb 09 '19 at 19:35

0 Answers0