[英]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.