For Loop doesn't spit out needed results
<p>I got this piece of code to spit out the unique "area number" in the URL. However, the loop doesn't work. It spits out the same number, please see below:</p>

<pre><code>import urllib3
from bs4 import BeautifulSoup

http = urllib3.PoolManager()

url = open('MS Type 1 URL.txt',encoding='utf-8-sig')

links = []
for link in url:
    y = link.strip()
    links.append(y)

url.close()

print('Amount of Links: ', len(links))

for x in links:
    j = (x.find("=") + 1)
    g = (x.find('&amp;housing'))
    print(link[j:g])
</code></pre>

<p>Results are:</p>

<p><a href="http://millersamuel.com/aggy-data/home/query_report?area=38&amp;housing_type=3&amp;measure=4&amp;query_type=quarterly&amp;region=1&amp;year_end=2020&amp;year_start=1980" rel="nofollow">http://millersamuel.com/aggy-data/home/query_report?area=38&amp;housing_type=3&amp;measure=4&amp;query_type=quarterly&amp;region=1&amp;year_end=2020&amp;year_start=1980</a>
23</p>

<p><a href="http://millersamuel.com/aggy-data/home/query_report?area=23&amp;housing_type=1&amp;measure=4&amp;query_type=annual&amp;region=1&amp;year_end=2020&amp;year_start=1980" rel="nofollow">http://millersamuel.com/aggy-data/home/query_report?area=23&amp;housing_type=1&amp;measure=4&amp;query_type=annual&amp;region=1&amp;year_end=2020&amp;year_start=1980</a>
23</p>

<p>As you can see it spits out the area number '23' which is only in one of this URL but not the '38' of the other URL.</p>

<p>There's a typo in your code. You iterate over <code>links</code> list and bind its elements to <code>x</code> variable, but print a slice of <code>link</code> variable, so you get the same string printed on each loop iteration. So you can change <code>print(link[j:g])</code> to <code>print(x[j:g])</code>, but it's better to call your variables with more descriptive names, so here's the fixed version of your loop:</p>

<pre><code>for link in links:
    j = link.find('=') + 1
    g = link.find('&amp;housing')
    print(link[j:g])
</code></pre>

<p>And I also want to show you a proper way to extract <code>area</code> value from URLs:</p>

<pre><code>from urllib.parse import urlparse, parse_qs
url = 'http://millersamuel.com/aggy-data/home/query_report?area=38&amp;housing_type=3&amp;measure=4&amp;query_type=quarterly&amp;region=1&amp;year_end=2020&amp;year_start=1980'
area = parse_qs(urlparse(url).query)['area'][0]
</code></pre>

<p>So instead of using <code>str.find</code> method, you can write this:</p>

<pre><code>for url in urls:
    parsed_qs = parse_qs(urlparse(url).query)
    if 'area' in parsed_qs:
        area = parsed_qs['area'][0]
        print(area)
</code></pre>

<p>Used functions:</p>

<ul>
<li><a href="https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlparse" rel="nofollow"><code>urllib.urlparse</code></a></li>
<li><a href="https://docs.python.org/3/library/urllib.parse.html#urllib.parse.parse_qs" rel="nofollow"><code>urllib.parse_qs</code></a></li>
</ul>

python python-3.x loops beautifulsoup urllib3