I can not figure out the output of the results of the elementary parser in python 3. I want to display a list of all links from a specific table of the web page, but I only get the first link from the list. I assume that I incorrectly deduce the data from the function get_all_links or incorrectly write them there:

 import requests from bs4 import BeautifulSoup def get_html(url): r = requests.get(url) return r.text def get_all_links(html): soup = BeautifulSoup(html, 'lxml') tds = soup.find('table', id='currencies').find_all('td', class_='currency-name') links = [] for td in tds: a = td.find('a').get('href') link = 'https://coinmarketcap.com' + a links.append(link) return links def main(): url = 'https://coinmarketcap.com' all_links = get_all_links( get_html(url) ) for i in all_links: print(i) if __name__ == '__main__': main() 

In the output I want to get a list of the type:

 https://coinmarketcap.com/currencies/bitcoin/ https://coinmarketcap.com/currencies/ethereum/ https://coinmarketcap.com/currencies/ripple/ 

etc.

In fact, I only get:

 https://coinmarketcap.com/currencies/bitcoin/ 

    1 answer 1

    In the get_all_links function get_all_links result is returned after the first iteration of the loop. You need to move the return links to the left.

     import requests from bs4 import BeautifulSoup def get_html(url): r = requests.get(url) return r.text def get_all_links(html): soup = BeautifulSoup(html, 'lxml') tds = soup.find('table', id='currencies').find_all('td', class_='currency-name') links = [] for td in tds: a = td.find('a').get('href') link = 'https://coinmarketcap.com' + a links.append(link) return links def main(): url = 'https://coinmarketcap.com' all_links = get_all_links( get_html(url) ) for i in all_links: print(i) if __name__ == '__main__': main()