简体   繁体   中英

'NoneType' object has no attribute 'text', beautifulsoup python

When i try to run the code and export it to csv. the error "'NoneType' object has no attribute 'text'" appears and I hace tried every way to fix it but nothing seems to be working.The ProductName Prints out but the Price is the one causing the error

    from bs4 import BeautifulSoup
    import requests
    import csv
    
    csv_file = open('CultBeauty.csv', 'w', encoding='utf-8')
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow(['Product Name','Price'])
    
    for i in range(10):
        url = requests.get('https://www.cultbeauty.com/skin-care.list?pageNumber={}&facetFilters=en_beauty_skincareSkinType_content:Dry'.format(i+1)).text
        soup = BeautifulSoup(url, 'lxml')
        lists = soup.find_all('div', class_= "productBlock")
        for list in lists:
            ProductName = list.find('h3', class_="productBlock_productName")
            ProductName= ProductName.text.strip()
            print(ProductName)
            Price = list.find('span', {'class' : "productBlock_priceValue"})
            Price = Price.text.strip()
            print(Price)
            csv_writer.writerow([ProductName,Price])
    csv_file.close()


output:
Paula's Choice Skin Perfecting 2% BHA Liquid Exfoliant (118ml)
36.00€
Paula's Choice Skin Perfecting 25% AHA and 2% BHA Exfoliant Peel 30ml
44.00€
Sunday Riley GOOD GENES Glycolic Acid Treatment 1.7 fl. oz.
140.00€
BYOMA Hydrating Serum 30ml
14.60€
Elemis Pro-Collagen Cleansing Balm 100g
51.90€
BYOMA Moisturising Gel Cream 50ml
13.50€
Paula's Choice Skin Perfecting 2% BHA Liquid Exfoliant - Trial Size (30ml)
12.00€
etc

but it doesn't save to csv file and brings out the error

Some list of items may not have price value.So you might use try except and also need to add user-agent . It always be better practice not using list as variable as list is a python keyword. Now it's working smoothly.

from bs4 import BeautifulSoup
import requests
import csv

csv_file = open('CultBeauty.csv', 'w', encoding='utf-8')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Product Name', 'Price'])
headers={"User-Agent":"mozilla/5.0"}
for i in range(10):
    url = requests.get('https://www.cultbeauty.com/skin-care.list?pageNumber={}&facetFilters=en_beauty_skincareSkinType_content:Dry'.format(i+1),headers=headers).text
    soup = BeautifulSoup(url, 'lxml')
    lists = soup.find_all('div', class_="productBlock")
    for lis in lists:
        ProductName = lis.find('h3', class_="productBlock_productName")
        ProductName = ProductName.text.strip()
        print(ProductName)
        try:
            Price = lis.find('span', {'class': "productBlock_priceValue"})
            Price = Price.text.strip()
            print(Price)
        except:
            pass
        csv_writer.writerow([ProductName, Price])
csv_file.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM