mihai
mihai

Reputation: 13

why am i receiving a print full of 'None'?

I am scraping a site, where i have a list of paragraphs inside a class. when i print the just the text inside the paragraphs is all fine. But if i want to store them into a list, i receive a None.

To go through all those paragraphs i use the methode find_all with a loop. How can i proceed to receive the real text, string and to store it into a list.

listagoala = []

col_moneda = container_fluid.find('div', class_='col-sm-2 hidden-xs')

moneda = col_moneda.find_all('p')

for paragraphs in moneda:
    listaplina = listagoala.append(paragraphs.text)
    print(listaplina)

OUTPUT:

None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None

Upvotes: 0

Views: 56

Answers (2)

xxbinxx
xxbinxx

Reputation: 1553

why not simply do

listagoala = []

col_moneda = container_fluid.find('div', class_='col-sm-2 hidden-xs')

moneda = col_moneda.find_all('p')

for paragraphs in moneda:
    listagoala.append(paragraphs.text)
    print(listagoala) # Using print here will print multiple lists, as this is inside loop

print(listagoala) # print the final list with all elements

You don't have to do

listaplina = listagoala.append(paragraphs.text)

listagoala.append(paragraphs.text) will work.


Just for your explanation:

>>> a = [1,2,3,4]
>>> b = [5,6,7,8]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6, 7, 8]
>>>

Upvotes: 1

NPE
NPE

Reputation: 500177

Note to the reader: This answer refers to the original version of the question that had the following line: listaplina = [paragraphs.text].extend(listagoala).

list.extend() doesn't return the modified list. It returns None (having modified the list in place).

It sounds like you're looking for the following:

listaplina = [paragraphs.text] + listagoala

Upvotes: 3

Related Questions