Reputation: 1163
The aim of this is to run through the contents of a HTML file and scout out all the img, input, area tags which don't have/have "alt" as one of their attributes. I've written the following for it. The libraries I'm using in Python are BeautifulSoup for extraction and urllib for opening the url. Posting only the relevant part.
alttrue = altfalse = []
multimedialist = ['img','input','area']
for tag in multimedialist:
for incodetag in soup.findAll(tag):
if incodetag.get('alt') is None:
altfalse.append(incodetag)
else:
alttrue.append(incodetag)
print(alttrue)
print(altfalse)
In the end, the code is able to find all img, input and area tags but when I print out alttrue and altfalse, both have the same img/input/area links even if there isn't an alt attribute in them!
Also, another question I have is, in Django, I'm returning these two lists to a calling function in my views.py. And I'm putting those 2 lists as well as a bunch of other lists into a variable and passing that variable to a html page using the render function. In my html file, I'm using a for loop and iterating over all the lists I received from my views.py and printing them out. However, for these 2 lists in particular, on the html page, it shows as blank lists ([]). But If I normally print the variable on the html page without using a for loop for each element, then it prints. There isn't a problem with how I'm passing the lists from my views.py to my html page because the others are working just fine. Why is it with this one though?
Upvotes: 0
Views: 59
Reputation: 30171
The alttrue and altfalse variables are both pointing at the same list, so appends to one will affect the other as well. You should create two separate lists:
alttrue = []
altfalse = []
Upvotes: 2