2016-10-10 102 views
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 

回答

0

找到解决方案! 我在做之前必须做一个测试(测试soup.find_all(class_="prdtBZPrice")非空):

prix_origin = soup.find_all(class_="prdtBZPrice")[i].find(class_="prdtPrSt").text.replace(u'\xa0', '')