Traceback most recent call last File CPython3靴py line 18 in module total_comment = soupfindspan class rating_numstringAttributeError NoneType object has no attribute string
This error is raised when the code tries to access the 'string' attribute of an object that is of type 'NoneType'. In this specific case, it means that the 'find' method of the BeautifulSoup object returned 'None', which means it couldn't find an element with the specified parameters.
To fix this error, you should check if the object returned by the 'find' method is not 'None' before trying to access its attributes. You can do this by using an 'if' statement like this:
total_comment = soup.find('span', {'class': 'rating_num'})
if total_comment is not None:
total_comment = total_comment.string
else:
total_comment = 'N/A'
This code checks if 'total_comment' is not 'None', and if it is not, it assigns the value of its 'string' attribute to 'total_comment'. If 'total_comment' is 'None', it assigns the value 'N/A' to 'total_comment' instead
原文地址: https://www.cveoy.top/t/topic/g7bf 著作权归作者所有。请勿转载和采集!