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
添加回答
舉報