简体   繁体   中英

Python - TypeError: object of type 'NoneType' has no len()

I wrote a code that strips two values from a set of 50 items from a website and stored them into a list. However, when I check to see how many values there are via len() I get the following error:

TypeError: object of type 'NoneType' has no len()

This is my code:

from datetime import datetime

response = links

def get_links(response):
    list_links = []
    for i in range(len(response)):
        url = response[i].select('a')[0]['href']
        date = datetime.strptime(response[i].select('span.field-content')[1].text, '%A, %B %d, %Y')
        list_links.append((url, date))

get_links(response)

page = get_links(response)

assert len(page) == 50

any ideas?

Your get_links function does not return anything add a return statement to it + iterating through the list is more optimal

here is an improved version of your code with the return statement

def get_links(response):
    list_links = []
    for link in response:
        url = link.select('a')[0]['href']
        date = datetime.strptime(link.select('span.field-content')[1].text, '%A, %B %d, %Y')
        list_links.append((url, date))
    return list_links

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM