BeautifulSoup to find text within <img

后端 未结 2 692
我在风中等你
我在风中等你 2021-01-14 23:25

Here is what I get back from this Python line of code

listm = soup.findAll(\'td\',{\'class\':\'thumb\'})

when I iterate over the listm, her

相关标签:
2条回答
  • 2021-01-14 23:49

    Try this:

    listm = soup.findAll('td',{'class':'thumb'})
    for elem in listm:
        print elem('img')[0]['alt']
    

    This should find the img tag within each td and print the values of the alt attribute.

    EDIT:

    You should not assume the existence of the img tag. Do this instead:

    listm = soup.findAll('td',{'class':'thumb'})
    for elem in listm:
        imgs = elem('img')
        if imgs:
            print imgs['alt']
    
    0 讨论(0)
  • 2021-01-14 23:56

    Your td elements contain some nested elements; search for the img tag within each td element, then take the alt attribute with an item lookup:

    for td in listm:
        img = td.find('img')
        if img is not None:
            print img['alt']
    
    0 讨论(0)
提交回复
热议问题