Append value to list with BeautifulSoup in Python
<pre><code>allname = []
alltel = []
allemail = []

for link in docdetail:
            name = soup.h1.contents[1]
            tel = soup.find('a', class_='tel').string
            email = soup.find('a', class_='email').string
            allname.append(name)
            alltel.append(tel)
            allemail.append(email)
</code></pre>

<p>I want to append name to allname, tel to alltel, email to allemail after every for loop. But it is possible that the returned value for name/tel/email is empty, then he order of attributes may mess up. 
I want to assign "NA" to the variable if the returned values are empty. How should I write a code for this precisely?</p>

<p>You can write your own function to verify the attribute, See below example.</p>

<pre><code>def validate_string(attr):
    if attr.strip() == '':
        return 'NA'
    else:
        return attr.strip()

allname = []
alltel = []
allemail = []

for link in docdetail:
    name = soup.h1.contents[1]
    tel = soup.find('a', class_='tel').string
    email = soup.find('a', class_='email').string
    allname.append(validate_string(name))
    alltel.append(validate_string(tel))
    allemail.append(validate_string(email))
</code></pre>

python beautifulsoup append