'NoneType' 对象没有属性 'text'、beautifulsoup python

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

当我尝试 运行 代码并将其导出到 csv 时。出现错误“'NoneType' object has no attribute 'text'”,我尝试了各种方法来修复它,但似乎什么都没有 working.The ProductName 打印出来,但价格是导致错误的原因

    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

有些商品列表可能没有价格 value.So 您可以使用 try except 并且还需要添加 user-agent 。最好不要将列表用作变量,因为列表是 python 关键字。现在一切顺利。

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()