Search code examples
pythonpython-3.xnameerror

Unfilled Parameter


I wrote this block of code to format a list of URL-links.

def main():
    format_link()
    create_html_link()

def format_link(dir_link_dirty):
    dir_link = dir_link_dirty.replace('"', "").replace(",", "").replace("\n", "")
    dir_link_code = urllib.request.urlopen(dir_link)
    bs_dir_link_code = BeautifulSoup(dir_link_code, "html5lib")
    h2_a_tag = bs_dir_link_code.h2.a
    html_link = str(dir_link) + "/" + str(h2_a_tag["href"])

    return html_link

def create_html_link():
    dir_lst = load_dir_file()
    for dir_link_dirty in dir_lst:
        html_link = str(format_link(dir_link_dirty))
        return html_link

If I run the code, I will receive this message:

format_link(str(dir_link_dirty))
NameError: name 'dir_link_dirty' is not defined
Process finished with exit code 1

What do I have to change to run it successfully?


Solution

  • When invoking format_link in main there isn't an argument for dir_link_dirty.

    broken:

    def main():
        format_link()
        create_html_link()
    

    fixed:

    def main():
        format_link("https://example.com")
        create_html_link()
    

    complete example:

    def main():
        create_html_link()
    
    def create_html_link():
        dir_lst = load_dir_file()
        for dir_link_dirty in dir_lst:
            html_link = str(format_link(dir_link_dirty))
            print(html_link)
            return html_link
    
    
    def format_link(dir_link_dirty):
        dir_link = dir_link_dirty.replace('"', "").replace(",", "").replace("\n", "")
        dir_link_code = urllib.request.urlopen(dir_link)
        bs_dir_link_code = BeautifulSoup(dir_link_code, "html5lib")
        h2_a_tag = bs_dir_link_code.h2.a
        html_link = str(dir_link) + "/" + str(h2_a_tag["href"])
        print(html_link)
        return html_link
    
    def load_dir_file():
        return ["https://www.gesetze-im-internet.de/ao_1977/BJNR006130976.html",
                "https://www.gesetze-im-internet.de/ao_1977/BJNR006130976.html"]