第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

錯誤 TypeError: 'NoneType' object is not iterab

錯誤 TypeError: 'NoneType' object is not iterab

海綿寶寶撒 2023-02-15 15:24:32
def parse():    html = get_html(URL)    if html.status_code == 200:        phones = []        pages_count = pages(html.text)        for page in range(1, pages_count + 1):            print(f'Parsing a page {page} from {pages_count}...')            html = get_html(URL, params={'p': page})            phones.extend(get_content(html.text))        print(phones)    else:        print('Error')嗨,我想列出項目,但出現(xiàn)錯誤  File "C:/Users/User/PycharmProjects/Parser/parser.py", line 52, in <module>    parse()  File "C:/Users/User/PycharmProjects/Parser/parser.py", line 46, in parse    phones.extend(get_content(html.text))TypeError: 'NoneType' object is not iterab這是所有代碼:import requestsfrom bs4 import BeautifulSoupURL = 'https://comfy.ua/smartfon/'HEADERS = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0',           'accept': '*/*'}def get_html(url, params=None):    r = requests.get(url, headers=HEADERS, params=params)    return rdef pages(html):    soup = BeautifulSoup(html, 'html.parser')    pagination = soup.find_all('li', class_='pager__number')    if pagination:        return int(pagination[-2].get_text())    else:        return 1def get_content(html):    soup = BeautifulSoup(html, 'html.parser')    items = soup.find_all('div', class_="product-item__i")    phone = []    for item in items:        phone.append({            'title': item.find('p', class_="product-item__name").get_text(strip=True),            'link': item.find('a', class_="product-item__name-link js-gtm-product-title").get('href'),            'price': item.find('div', class_="price-box__content-i").get_text(strip=True).replace(u'\xa0', u' ')        })    print(phone)我得到一個空列表,但應(yīng)該拿到電話。我也收到一個錯誤。
查看完整描述

1 回答

?
瀟瀟雨雨

TA貢獻(xiàn)1833條經(jīng)驗 獲得超4個贊

phones.extend(get_content(html.text))

TypeError: 'NoneType' object is not iterab

此錯誤告訴您您正在嘗試迭代None. 由于extend()需要一個可迭代對象,因此這告訴您get_content()正在返回None。當(dāng)函數(shù)什么都不返回時經(jīng)常會發(fā)生這種情況:沒有 return 語句等同于return NonePython。


果然,您的代碼get_content()沒有返回語句。您需要添加它:


def get_content(html):

    soup = BeautifulSoup(html, 'html.parser')

    items = soup.find_all('div', class_="product-item__i")


    phone = []

    for item in items:

        phone.append({

            'title': item.find('p', class_="product-item__name").get_text(strip=True),

            'link': item.find('a', class_="product-item__name-link js-gtm-product-title").get('href'),

            'price': item.find('div', class_="price-box__content-i").get_text(strip=True).replace(u'\xa0', u' ')

        })

    print(phone)

    return phone  #   <--- add this


查看完整回答
反對 回復(fù) 2023-02-15
  • 1 回答
  • 0 關(guān)注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號