BeautifulSoup: Extract "img alt" content Web Scraping in Python
Question: I am working in python 3. My objective is extracting differents values of one
table and to put them in differents lists.

The problem is that i can't take the value of "img alt" in a td.

This is my code:

    
    
        from bs4 import BeautifulSoup
    import urllib.request
    
    redditFile = urllib.request.urlopen("http://www.mtggoldfish.com/movers/online/all")
    redditHtml = redditFile.read()
    redditFile.close()
    soup = BeautifulSoup(redditHtml)
    all_tables = soup.find_all('table')
    
    right_table = soup.find('table', class_='table table-bordered table-striped table-condensed movers-table')
    
    #create a list
    A=[]
    B=[]
    C=[]
    D=[]
    
    for row in right_table.findAll("tr"):
        cells = row.findAll('td')
        increment = row.findAll('span')
        colection = row.findAll('img')
        link = row.findAll('a')
        if len(cells) == 6:
            A.append(cells[0].find(text=True))
            B.append(increment[0].find(text=True))
            C.append(colection[0])
            D.append(link[0].find(text=True))
    print(A)
    print(B)
    print(C)
    print(D)
    

This code gives me this result:

    
    
    ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
    ['+8.40', '+2.47', '+1.35', '+1.28', '+1.14', '+0.99', '+0.94', '+0.91', '+0.90', '+0.75']
    [<img alt="ORI" class="sprite-set_symbols_ORI" src="//assets1.mtggoldfish.com/assets/s-407aaa9c9786d606684c6967c47739c5.gif"/>, <img alt="PRM" class="sprite-set_symbols_PRM" src="//assets1.mtggoldfish.com/assets/s-407aaa9c9786d606684c6967c47739c5.gif"/>, <img alt="8ED" class="sprite-set_symbols_8ED" src="//assets1.mtggoldfish.com/assets/s-407aaa9c9786d606684c6967c47739c5.gif"/>, <img alt="EX" class="sprite-set_symbols_EX" src="//assets1.mtggoldfish.com/assets/s-407aaa9c9786d606684c6967c47739c5.gif"/>, <img alt="TSB" class="sprite-set_symbols_TSB" src="//assets1.mtggoldfish.com/assets/s-407aaa9c9786d606684c6967c47739c5.gif"/>, <img alt="WL" class="sprite-set_symbols_WL" 
    

src="//assets1.mtggoldfish.com/assets/s-407aaa9c9786d606684c6967c47739c5.gif"/>,
, , , ] ["Jace, Vryn's Prodigy", "Gaea's Cradle", 'Ensnaring Bridge', 'City of
Traitors', 'Pendelhaven', 'Firestorm', 'Kor Spiritdancer', 'Scalding Tarn',
'Daybreak Coronet', 'Grove of the Burnwillows']

But I need the IMG ALT VALUE in (for exemple the first img alt value is "ORI")

> colection variable

**I don't have any idea that I can do. Guys, could you help me with this,
please?**

Thanks so much in advance


Answer: Once you have an `<img>` node instance, you can get the alt value using this:

    
    
    alt_tag = img.attrs['alt']
    

Since you're getting a collection of img elements, you can iterate over it and
retrieve the alt tag for each:

    
    
    tags = []
    collection = soup.findAll("img")
    for img in collection:
        if 'alt' in img.attrs:
            tags.append(img.attrs['alt'])
    #do whatever you need to do with your list of alt attributes.
    print tags 
    

