BeautifulSoup Finding Subdirectories
Question: I'm trying to figure out how to find sub-directories on a webpage with
BeautifulSoup in Python. I have an idea on how I would do it. This is what I
had in mind:

    
    
    from bs4 import BeautifulSoup
    
    html = '''<a href="/images/pic.png">images</a>
    <a href="google.com">google</a>'''
    
    soup = BeautifulSoup(html)
    links = soup.find_all('a', href=True)
    for link in links:
        print a['href']
    

Above would post all links on a page though. How would I get it to just print
back sub-directories like in the example "/images/pic.png"?

Although I want to use beautifulsoup using any other module would be fine.


Answer: Add a `if` condition for `a['href']`, for example with the assumption that
sub-directories will at least have two `/` in the path, you can use
`a['href'].count('/') >= 2` as a condition.

Sample:

    
    
    from bs4 import BeautifulSoup
    html = '''<a href="/images/pic.png">images</a>
    <a href="google.com">google</a>'''
    
    soup = BeautifulSoup(html)
    links = soup.find_all('a', href=True)
    for link in links:
        if a['href'].count('/') >= 2:
            print a['href']
    

If you mean relative path by "sub-directories", you can use
`a['href'].startswith('/')` as a condition.

