Reputation: 109
I am facing an error that I am unable to fix although there are similar questions none of the solutions were solving it for me so I decided to ask the question:
I am trying to web scrape a website for an email in the biography of users, if the UserInfo contains an email it should return, but what I am receiving is an empty list [].
I have searched several answers to get rid of the empty lists but none of them were successful
Here is part of the code:
email_list = []
email=None
while email == None:
if soup.find_all(class_="UserInfo "):
for my_tag in soup.find_all(class_="UserInfo "):
emails = re.findall(EMAIL_REGEX, my_tag.text)
print (emails)
if emails != []: # if tag is empty
if emails:
email = emails[0]
print(email)
email_list.append(email)
print(email_list, "1")
break
else:
email=None
print("None 1 in User Info")
Here is part of HTML: The UserInfo class can be duplicated but all of them could be missing the email:
<div class="UserInfo ">
<div>
<div class="ReadMore">text</div>
<a class="UserInfo-readMoreOrLessText">Read Less</a>
</div>
</div>
I have tried the following but didn’t work:
email = [x for x in emails if x]
print(email)
It returned
[]
I have also tried
if not emails: # if tag is empty
emails.decompose()
print(emails)
But returned
AttributeError: 'list' object has no attribute 'decompose'
Upvotes: 1
Views: 117
Reputation: 1807
What if you set emails
to None
if emails
is an empty list.
emails = None if not emails else emails
if not emails:
break
# Other logic here.
Upvotes: 1