Duplicate element from for loop in BeautifulSoup
<p>Given the code:</p>

<pre><code>import urllib
import urllib.request
from bs4 import BeautifulSoup
from urllib.request import urlopen

def make_soup(url):
    thepage = urllib.request.urlopen(url)
    soupdata = BeautifulSoup(thepage, "html.parser")
    return soupdata

soup = make_soup("https://www.wellstar.org/locations/pages/wellstar-acworth-practices.aspx")
for table in soup.findAll("table", class_ = "s4-wpTopTable"):
    for specialty in table.findAll("div", class_ = "PurpleBackgroundHeading"):
        specialty = specialty.get_text(strip = True)
for name in table.findAll(class_ = "WS_Location_Name"):
    name = name.get_text()
    print(specialty, " - ", name)
</code></pre>

<p>This code yields the properly looped location name coupled with the improperly looped specialty name.  For example, the previous code produces:</p>

<pre><code>Urology - Center for Spine Interventions, PC
Urology - WellStar Medical GroupCardiovascular Medicine
</code></pre>

<p>With <code>Urology - Georgia Urology</code> being the last couple produced.  How can I be sure to create pairs of Specialties and Location Names that correspond to reality?</p>

<p>could it be the indentation?
How should the for loops be nested?
in python I would expect something like:</p>

<pre><code>for tabele ...:
    for specialty ...:
        spe..
    for name ...:
        name ...
        print ...
</code></pre>

<p>or</p>

<pre><code>for tabele ...:
    for specialty ...:
        spe..
        for name ...:
            name ...
            print ...
</code></pre>

python web-scraping beautifulsoup