Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Will Berlanga
Will Berlanga
18,825 Points

name 'site_links' is not defined

I tried running the teacher's code, but I keep getting an error that 'site_links' is undefined. I tried changing the variable to a different name thinking maybe it was a reserved word, but that didn't help. I also tried disabling the pylint error. I am running Python version 3.7.6 Here is the code I am trying to run:

from urllib.request import urlopen
from bs4 import BeautifulSoup

import re

def internal_links(linkURL):
    html = urlopen(
        'https://treehouse-projects.github.io/horse-land/{}'.format(linkURL))
    soup = BeautifulSoup(html, 'html.parser')

    return soup.find('a', href=re.compile('(.html)$'))


if __name__ == '__main__':
    urls = internal_links('index.html')
    while len(urls) > 0:
        page = urls.attrs['href']
        if page not in site_links:
            site_links.append(page)

            print(page)
            print('\n============\n')
            urls = internal_links(page)
        else:
            break

2 Answers

Josh Keenan
Josh Keenan
20,315 Points

You never defined the site links array. Line 7 of his code has

site_links = []
Josh Keenan
Josh Keenan
20,315 Points

I don't know what video you are going from but at no time do you define site_links.

You first reference it here

if page not in site_links:

Could you have renamed the variable to urls and not updated that everywhere?

Will Berlanga
Will Berlanga
18,825 Points

The video is Everyone Loves Charlotte (https://teamtreehouse.com/library/everyone-loves-charlotte) from the Scraping Data from the Web Course.