简体   繁体   English

Python-TypeError:“ NoneType”类型的对象没有len()

[英]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. 我编写了一个代码,该代码从网站的一组50个项目中剥离两个值,并将它们存储到列表中。 However, when I check to see how many values there are via len() I get the following error: 但是,当我通过len()检查有多少个值时,出现以下错误:

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 您的get_links函数不返回任何内容,向其添加一个return语句+遍历列表更佳

here is an improved version of your code with the return statement 这是带有return语句的代码的改进版本

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python TypeError:“NoneType”类型的对象没有 len() - Python TypeError: object of type 'NoneType' has no len() Python TypeError: (“'NoneType' 类型的对象没有 len()” - Python TypeError: (“object of type 'NoneType' has no len()” Python:TypeError:'NoneType' 类型的对象没有 len() - Python: TypeError: object of type 'NoneType' has no len() TypeError:类型为'NoneType'的对象没有len()python - TypeError: object of type 'NoneType' has no len() python TypeError:“NoneType”类型的对象在应用程序部署中没有 len() Python - TypeError: object of type 'NoneType' has no len() Python on application deployment Python tkinter TypeError:类型为“ NoneType”的对象没有len() - Python tkinter TypeError: object of type 'NoneType' has no len() Python 错误:“TypeError:“NoneType”类型的 Object 没有 len()” - Python error: “TypeError: Object of type 'NoneType' has no len()” TypeError:“NoneType”类型的 object 没有 len(),在我的 python 代码中 - TypeError: object of type 'NoneType' has no len() ,in my python code Python PWManager 出现 TypeError 问题:“NoneType”类型的 object 没有 len() - Python PWManager problem with TypeError: object of type 'NoneType' has no len() Python错误“ TypeError:类型为'NoneType'的对象没有len() - Python error "TypeError: object of type 'NoneType' has no len()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM