Add elements in Dictionary and convert them to string

Asked

Viewed 80 times

0

Hello,

I am doing some tests with Scraping using the library "Beautifulsoup" python and I came up with a question. I was able to extract some information from a site like product title, sku, and its technical information, but I needed to add it to a dictionary and then convert these results to string.

follows the code I’m using:

titulo=soup.find(class_='fbits-produto-nome prodTitle title').get_text()

sku=soup.find(class_='fbits-sku').get_text()


descri=soup.find('div',{'class':'conteudoAbasProduto'}).children.get_text()

desc={'titulo':titulo,'sku':sku,'descri':descri}

print(desc)"

but when I print it brings me that result:

"Attributeerror: 'list_iterator' Object has no attribute 'get_text"

can help me?

  • if you print the variables: title, sku, describe individually they are with the data?

  • are yes, but when I print the dictionary it brings nothing

1 answer

0

descricao=soup.find('div',{'class':'conteudoAbasProduto'}).children

The variable descricao above returns an object list iterator:

<list_iterator object at 0x7f9c14b99908>

The text you are looking for is contained in one of the variable elements descricao. Thus, it is necessary to iterate on this variable. If it is not possible to iterate directly through a for, just cast a list(descricao).

Finally, you can print each of the elements of the generated list and check in which case the method getText() is available:

for element in descricao:
    try:
        element.getText()
        print(element)
    except AttributeError:
        continue

Browser other questions tagged

You are not signed in. Login or sign up in order to post.