python - 错误 TypeError : 'NoneType' object is not iterable mean? 是什么意思

标签 python

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 函数可能不返回任何结果。如果发生这种情况,该函数将返回非类型,有点像 python 的 null 。然后你试图对不存在的东西进行 for 循环。代码中有几个区域可能会引发此错误,但基本上这仅意味着 for 循环中表达式 IN 之后的变量没有值。你可以做一个。 if soup.find_all(class_ = "album_tit") is NoneType: print("查找所有不返回值的函数")

关于python - 错误 TypeError : 'NoneType' object is not iterable mean? 是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42732890/

相关文章:

python - 有没有办法在 python pandas 中向量化投资组合标准差

python - 提高代码效率: standard deviation on sliding windows

python 3 : generator for map

python - 如何从保存的模型(SSD MobileNet)创建 tflite 文件

python - mypy:是否可以为复杂类型定义快捷方式?

python - 使用套接字向单人游戏添加多人游戏功能

python - 时间序列数据中的峰谷

Python Selenium : select option

python - 使用 pyinstaller 和 pysqlcipher 创建一个文件 exe 时出现问题

python - OpenCV掩码错误错误:(-215:声明失败)(mtype == CV_8U || mtype == CV_8S)&& _mask.sameSize(* psrc1)在函数'cv::binary_op中