Beautiful Soup - Python
<p>I was hoping to ask a pretty simple question. I have come across the below code and have not been able to find a decent explanation as to:</p>

<ol>
<li>What exactly does the <code>.attrs</code> function do in this case?</li>
<li>What is the function of the <code>['href']</code> part at the end i.e. what exactly does that part of the code execute?</li>
</ol>

<p>Here is the code:</p>

<pre><code>from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("url")
bsObj = BeautifulSoup(html)
for link in bsObj.findAll("a"):
    if 'href' in link.attrs:
    print (link.attrs['href']) 
</code></pre>

<p>Let's go line by line after imports</p>

<ul>
<li>Load a url into variable called html</li>
<li>Create BeautifulSoup object from html</li>
<li>For every link in the object's "a" tags (it loops over html tags in the html, finds all <code>&lt;a&gt;</code> and loops over them)</li>
<li>If the attribute of the tag has 'href' (<code>&lt;a href=""&gt;</code> - href is an attribute, thatt's stored in the link's .attrs property)</li>
<li>print to stdout the attribute that has key 'href' (it's a dictionary with <code>'href':'http://something'</code>)</li>
</ul>

<p>The indentation is a bit wrong there, print should have be more indented than if</p>

python