2016-10-10 3 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) 

und es funktioniert. Aber wenn ich versuche, es in einer for-Schleife zu tun, habe ich diese Meldung: Attribute: ‚NoneType‘ Objekt hat kein Attribut ‚Text‘Delete Html Balise in einem BeautifulSoup Findall

hier ist mein Code:

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 

Antwort

0

gefunden Lösung! Ich musste einen Test machen (Test soup.find_all(class_="prdtBZPrice") nicht Null), bevor Sie tun:

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