Найти - Пользователи
Полная версия: Помощь в return
Начало » Python для новичков » Помощь в return
1
casiy
Код:
 from urllib.request import urlopen
from bs4 import BeautifulSoup
def fun():
    quest = 'https://python.org/'
    url = urlopen(quest).read()
    soup = BeautifulSoup(url, "html.parser")
    for links_a in soup.find_all('a'):
        return links_a.get('href')
if __name__ == '__main__':
    print(fun())

Почему возвращает только одну ссылку а не все со страницы.Подскажите.
JOHN_16
потому что return выходит из функции, тем самым прерывая ее выполнение
casiy
А как тогда можно это сделать,чтоб вызвать эту функцию и получить все линки.
izekia
 from urllib.request import urlopen
from bs4 import BeautifulSoup
  
def fun():
    quest = 'https://python.org/'
    url = urlopen(quest).read()
    soup = BeautifulSoup(url, "html.parser")
    for links_a in soup.find_all('a'):
        yield links_a.get('href')
  
if __name__ == '__main__':
    print(list(fun()))
casiy
Спасибо всем.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB