Home > database >  In the 'for' cycle, when using '.append', it turns out 'none'
In the 'for' cycle, when using '.append', it turns out 'none'

Time:12-12

When adding new information to the list using '.append', I get none.

data = []
for e in movie:
    ru_name = print(e.find('div', class_='base-movie-main-info_mainInfo__ZL_u3').find('span', class_='styles_mainTitle__IFQyZ styles_activeMovieTittle__kJdJj').text)
    original_name = print(e.find('span', class_='desktop-list-main-info_secondaryTitle__ighTt').text)
    remain = print(e.find('div', class_='styles_main__Y8zDm styles_mainWithNotCollapsedBeforeSlot__x4cWo').find('span', class_='desktop-list-main-info_truncatedText__IMQRP').text)
    rate = print(e.find('span', class_='styles_kinopoiskValuePositive__vOb2E styles_kinopoiskValue__9qXjg styles_top250Type__mPloU').text)
    link = print("https://www.kinopoisk.ru" e.find('a',class_= 'base-movie-main-info_link__YwtP1').get('href'))
    
    data.append([ru_name, original_name, remain, rate, link])

I don't understand why none is on the list. I looked at a lot of topics on this question, and it seems like I have everything right, at the end of the cycle without a 'print', just adding a 'date.append' to the list. If I add to the list before the cycle, then everything is displayed correctly. I can't understand why this is happening.

CodePudding user response:

The print function returns None so any value given to the print function just returns None. So ru_name = print(e.find(...) also return the None. Means ru_name is set to None.

data = []
for e in movie:
    ru_name = e.find(...
    original_name = e.find(...
    remain = e.find(...
    rate = e.find(...
    link = "https://ww.kinopoisk.ru"   e.find(...

    ## if you also want to print these then

    print(ru_name)
    print(original_name)
    print(remain)
    print(rate)
    print(link)

    ## Append to the list
    data.append([ru_name, original_name, remain, rate, link])

CodePudding user response:

Just type in REPL (or in python script) and run it:

x=print("whatever")
print(x)

print function has None as the return type, so it's expected to happen.

This:

data.append([ru_name, original_name, remain, rate, link])

Does exactly the same as this:

data.append([None, None, None, None, None])

In order to achieve what you want there are at least 2 options:

  • assign and then print
link = "https://www.kinopoisk.ru" e.find('a',class_= 'base-movie-main-info_link__YwtP1').get('href')
print(link)
  • use walrus to both assign and print at the same time (I wouldn't recommend that to be honest, probably that's not the intended use of walrus, but it's doable...)
print(f"{(link:='<your_whole_string_goes_here>')}")

And then append as you do right now (no changes required, since in both cases link is your string, not a None anymore.

  • Related