2x 嵌套函数中的访问列表


def get_html(url):
    response = urllib.request.urlopen(url)
    return response.read()

def parse_main(html):
    webpage = BeautifulSoup(html, features="html.parser")
    table = webpage.find('table', id='itemList')
    for a_tag in table.find_all('a', class_='all'):
        parse_movie(get_html('https://www.somerandommovieswebsite.com' + a_tag['href']))

def parse_movie(html):
    web_page = BeautifulSoup(html, features="html.parser")
    info = web_page.find('h1', class_="moviename")
    movies.append(info.text)

def main():
    movies = []
    parse_main(get_html('https://www.somerandommovieswebsite.com'))
    print(movies)

if __name__ == '__main__':
    main()

如何访问嵌套parse_main中的电影列表(在 main(( 函数中定义parse_movie。由于">未解析的引用'电影'"错误,无法将任何内容附加到列表中。使用非本地没有帮助

我认为您既不应该在这里使用全局变量,也不应该将其作为参数传递:

def get_html(url):
    response = urllib.request.urlopen(url)
    return response.read()

def parse_main(html):
    movies = []
    webpage = BeautifulSoup(html, features="html.parser")
    table = webpage.find('table', id='itemList')
    for a_tag in table.find_all('a', class_='all'):
        movies.append(
            parse_movie(get_html('https://www.somerandommovieswebsite.com' + a_tag['href']))
        )
    return movies

def parse_movie(html):
    web_page = BeautifulSoup(html, features="html.parser")
    info = web_page.find('h1', class_="moviename")
    return info.text

def main():
    movies = parse_main(get_html('https://www.somerandommovieswebsite.com'))
    print(movies)

if __name__ == '__main__':
    main()

有几种方法可以做到这一点。

首先定义全局电影。

其次,您可以将列表作为这样的参数传递。

由于列表是通过引用传递的,并且我们正在附加在 main 函数中定义的列表,我们不需要返回到 main 函数。

def parse_main(html,movies):
    webpage = BeautifulSoup(html, features="html.parser")
    table = webpage.find('table', id='itemList')
    for a_tag in table.find_all('a', class_='all'):
        parse_movie(get_html('https://www.somerandommovieswebsite.com' + a_tag['href']),movies)

def parse_movie(html,movies):
    web_page = BeautifulSoup(html, features="html.parser")
    info = web_page.find('h1', class_="moviename")
    movies.append(info.text)

def main():
    movies = []
    parse_main(get_html('https://www.somerandommovieswebsite.com'),movies)
    print(movies)

第三种方法是在函数内创建一个列表并返回

def parse_main(html):
    webpage = BeautifulSoup(html, features="html.parser")
    table = webpage.find('table', id='itemList')
    movies = []
    
    for a_tag in table.find_all('a', class_='all'):
        movies.append (parse_movie(get_html('https://www.somerandommovieswebsite.com' + a_tag['href'])))
    return movies
    
def parse_movie(html):
    web_page = BeautifulSoup(html, features="html.parser")
    info = web_page.find('h1', class_="moviename")
    return info.text
def main():
    movies = parse_main(get_html('https://www.somerandommovieswebsite.com'))
    print(movies)

最简单的方法是使用全局变量。但应尽可能避免使用全局变量。您可以像这样更改代码,避免使用全局变量并将变量作为参数传递。

def get_html(url):
    response = urllib.request.urlopen(url)
    return response.read()

def parse_main(html):
    parse_movies = []
    webpage = BeautifulSoup(html, features="html.parser")
    table = webpage.find('table', id='itemList')
    for a_tag in table.find_all('a', class_='all'):
        parse_movies.append(parse_movie(get_html('https://www.somerandommovieswebsite.com' + a_tag['href'])))
    return movies

def parse_movie(html):
    web_page = BeautifulSoup(html, features="html.parser")
    info = web_page.find('h1', class_="moviename")
    return info.text

def main():
    movies = parse_main(get_html('https://www.somerandommovieswebsite.com'))
    print(movies)

if __name__ == '__main__':
    main()

movies列表作为参数传递并避免使用全局变量,在大多数情况下更好。

问题是movies是 ̀ parse_movie 内部的局部变量,这意味着它与main中定义的变量不同。

我只是简单地将 ̀ movies 变量从 main 函数向下传递到parse_movie函数,并添加了return语句。

def get_html(url):
    response = urllib.request.urlopen(url)
    return response.read()

def parse_main(html):
    movies = []
    webpage = BeautifulSoup(html, features="html.parser")
    table = webpage.find('table', id='itemList')
    for a_tag in table.find_all('a', class_='all'):
        movies.append(parse_movie(get_html('https://www.somerandommovieswebsite.com' + a_tag['href'])))
    return movies

def parse_movie(html):
    web_page = BeautifulSoup(html, features="html.parser")
    info = web_page.find('h1', class_="moviename")
    return info.text

def main():
    movies = parse_main(get_html('https://www.somerandommovieswebsite.com'))
    print(movies)

if __name__ == '__main__':
    main()

movies 是 main 函数中的一个局部变量,所以你的函数找不到它是正常的,要么让它全局(并不总是一个好主意(要么把它作为一个参数传递。

最新更新