Webscraping - TypeError: 'NoneType' object is not subscriptable

I want the function to collect all the titles from the img tags and all the text from the h3 tags. The output of the loop is:

"TypeError: 'NoneType' object is not subscriptable"`.

Can someone tell me what did I do wrong?


url = "https://www.historico.portugal.gov.pt/pt/o-governo/arquivo-historico/governos-constitucionais/gc18/composicao.aspx"

uClient = urlopen(url)

soup = BeautifulSoup(uClient.read(), "html.parser")

containers = soup.findAll("li")

container = containers[7]

for container in containers:
    name = container.img["alt"]
    j = container.findAll("h3", {"class":"mainForecolor"})
    job = c[0].text

    print("nome: " + name)
    print("cargo: " + job)

Upvotes: 1

Views: 1652

Answers (2)

bkyada
bkyada

Reputation: 332

you can retrieve names as below.

your container is of type <class 'bs4.element.Tag'> So you will need to iterate through it and then look for another tag img which is again of type <class 'bs4.element.Tag'> So, to retrieve any attribute/property of that tag you will need to iterate through it.

url = "https://www.historico.portugal.gov.pt/pt/o-governo/arquivo-historico/governos-constitucionais/gc18/composicao.aspx"

uClient = urlopen(url)

soup = BeautifulSoup(uClient.read(), "html.parser")

container = soup.find_all('li')

for c in container:
    for link in c.findAll('img'):
        print("name : " +link.get('alt')) 

Or if you want to skip the Li tag at all you can directly find all the img tags and work as below.

container = soup.find_all('img')

for c in container:
    print("name : "+c.get('alt'))

Upvotes: 1

xibalba1
xibalba1

Reputation: 544

container.img does not exist, and thus is of the type None which cannot be subscripted in the manner container.img['alt'].

Why does container.img not exist? Well, container is going to be a BeautifulSoup Tag object which does not have an img property. Maybe you meant to access the attributes of the tag via container['img']. Unfortunately, for the example you've provided, none of the container Tags in containers has any attributes.

See: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#tag

Upvotes: 0

Related Questions