0
result = requests.get('http://www.cdiscount.com/search/10/ordinateur.html#_his_')
soup = BeautifulSoup(result.text, 'html.parser')
prix_origin = soup.find_all(class_="prdtBZPrice")[0].find(class_="prdtPrSt").text.replace(u'\xa0', '')
print(prix_origin)
它工作。 但是,如果我嘗試做一個for循環中,我有這樣的信息: AttributeError的:「NoneType」對象有沒有屬性「文本」在BeautifulSoup findall中刪除Html應答器
這裏是我的代碼:
nb = soup.find_all(class_="prdtBZPrice")
reductions = {}
for i in range(len(nb)) :
#print (i)
prix_origin = soup.find_all(class_="prdtBZPrice")[i].find(class_="prdtPrSt").text.replace(u'\xa0', '')
str(prix_origin).replace(u'\xa0','')
if prix_origin != None :
prix_reduit = soup.find_all(class_="prdtBZPrice")[i].find(class_="price").text.replace(u'\xa0', '')
reductions[prix_origin] = prix_reduit