This question already has answers here:
TypeError: 'NoneType' object is not iterable in Python
                                
                                    (7个答案)
                                
                        
                                2年前关闭。
            
                    
from bs4 import BeautifulSoup
import requests
import time

urls = ['http://www.soku.com/search_playlist/q_python_orderby_1_limitdate_0?site=14&page={}&spm=a2h0k.8191403.0.00'.format(str(i)) for i in range(1,30,1)]

def UUrl(urls):

    def Url(url):
        single_urls = []
        time.sleep(1)
        wb_data = requests.get(url)
        soup = BeautifulSoup(wb_data.text,'lxml')
        for single_urls  in soup.find_all(class_ = "album_tit"):
            single_url = (single_urls.a.get('href'))
            return single_url
            # print(single_url)

    for url in urls:
        Url(url)

def get_url_title(urls,data = None):
    urlsss = UUrl(urls)
    for surl in urlsss:
        wb_data = requests.get(surl)
        soup = BeautifulSoup(wb_data.text,'lxml')
        urlss = soup.find_all(class_="title short-title")
        titles = soup.find_all(class_="title short-title")

        for t_url,title in zip(urlss,titles):
            data = {
                 'title':title.get_text(),
                 'url': (t_url.a.get('href'))
            }
            print(data)

get_url_title(urls)

最佳答案

这意味着您正在遍历一个空值。 soup.findall函数可能未返回任何结果。如果发生这种情况,该函数将返回nonetype,有点像python的null。然后,您尝试对不存在的对象进行for循环。您的代码中有几个区域可能会引发此错误,但基本上,这仅意味着for循环中IN表达式后的变量没有值。你可以做一个。如果soup.find_all(class_ =“ album_tit”)为NoneType:print(“查找所有函数不返回值”)

关于python - 错误TypeError:“NoneType”对象不是可迭代的意味着什么? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42732890/

10-12 22:32