BeautifulSoup to find text within <img

后端 未结 2 691
我在风中等你
我在风中等你 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']
    

提交回复
热议问题