title: Following Links in HTML using BeautifulSoup
body: <p>I am doing a course which requires me to parse this using BeautifulSoup: <a href="http://python-data.dr-chuck.net/known_by_Fikret.html" rel="nofollow noreferrer">http://python-data.dr-chuck.net/known_by_Fikret.html</a> </p>
  
  <p>The instructions are: Find the link at position 3 (the first name is 1). Follow that link. Repeat this process 4 times. The answer is the last name that you retrieve.</p>
  
  <p>This is the code I have so far:</p>
  
  <pre><code>import urllib.request, urllib.parse, urllib.error
  from bs4 import BeautifulSoup
  import re
  
  url = input('Enter - ')
  html = urllib.request.urlopen(url).read()
  soup = BeautifulSoup(html, 'html.parser')
  
  count = int(input('Enter count: '))
  pos = int(input('Enter position: ')) - 1
  urllist = list()
  taglist = list()
  
  tags = soup('a')
  
  for i in range(count):
      for tag in tags:
          taglist.append(tag)
      url = taglist[pos].get('href', None)
      print('Retrieving: ', url)
      urllist.append(url)
  print('Last URL: ', urllist[-1])
  </code></pre>
  
  <p>This is my output:</p>
  
  <pre><code>Retrieving:  http://python-data.dr-chuck.net/known_by_Fikret.html 
  Retrieving:  http://python-data.dr-chuck.net/known_by_Montgomery.html
  Retrieving:  http://python-data.dr-chuck.net/known_by_Montgomery.html
  Retrieving:  http://python-data.dr-chuck.net/known_by_Montgomery.html
  Retrieving:  http://python-data.dr-chuck.net/known_by_Montgomery.html
  Last URL:  http://python-data.dr-chuck.net/known_by_Montgomery.html
  </code></pre>
  
  <p>This is the output that I am supposed to get:</p>
  
  <pre><code>Retrieving: http://python-data.dr-chuck.net/known_by_Fikret.html
  Retrieving: http://python-data.dr-chuck.net/known_by_Montgomery.html
  Retrieving: http://python-data.dr-chuck.net/known_by_Mhairade.html
  Retrieving: http://python-data.dr-chuck.net/known_by_Butchi.html
  Retrieving: http://python-data.dr-chuck.net/known_by_Anayah.html
  Last URL:  http://python-data.dr-chuck.net/known_by_Anayah.html
  </code></pre>
  
  <p>I've been working on this for a while but I still have not been able to get the code to loop correctly. I am new to coding and I'm just looking for some help to point me in the right direction. Thanks.</p>