简体   繁体   English

错误消息说“AttributeError:'NoneType' object 没有属性'text'”

[英]Error message saying "AttributeError: 'NoneType' object has no attribute 'text'"

Why is it giving me this error message saying AttributeError: 'NoneType' object has no attribute 'text'?为什么它给我这个错误消息说 AttributeError: 'NoneType' object has no attribute 'text'? Is there something wrong with the code?代码有问题吗?

from playwright.sync_api import sync_playwright
from bs4 import BeautifulSoup

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False, slow_mo=50)
    page = browser.new_page()
    page.goto('https://www.vans.com.sg/customer/account/login/')
    page.fill('input#email', 'abc')
    page.fill('input#pass', 'abc')
    page.click('button#send2')
    page.goto('https://www.vans.com.sg/men/clothing.html')
    page.is_visible('div.layer-product-list')
    html = page.inner_html('#layer-product-list')
    #print(html)

    soup = BeautifulSoup(html, 'html.parser')
    clothes = soup.find_all('li')

    for soup in clothes:

        price = soup.find('span', {'class' : 'price'}).text
        title = soup.find('a', {'class' : 'product-item-link'}).text

        print(f'Title = {title}, Price = {price}')

Here is the Terminal Output.这是终端 Output。 The error message says AttributeError: 'NoneType' object has no attribute 'text'错误消息显示AttributeError: 'NoneType' object has no attribute 'text'

....(other lists above)
Title = Vans x Curren x Knost Reversible Boardshort, Price = $99.00
Traceback (most recent call last):
  File "c:\Users\Ryan\Documents\Python Projects\loginscrape.py", line 21, in <module>
    price = soup.find('span', {'class' : 'price'}).text
AttributeError: 'NoneType' object has no attribute 'text'

Here is the html code for the elements这是元素的 html 代码

<li class="item product product-item"> <div class="product-item-info" data-container="product-grid">  <a href="https://www.vans.com.sg/comfycush-pullover-0a4ooo.html?color=3534" class="product photo product-item-photo" tabindex="-1" data-colorcode="3534" data-pid="">  <span class=" hover-image product-image-container" style="width:500px;"><span class="product-image-wrapper" style="padding-bottom: 100%;"><img src="https://ddpzd2b97pj2g.cloudfront.net/pub/media/catalog/product/medium/VN0A4OOOBLK1_ComfycushPullover_2.jpg"></span></span>  <span class="product-image-container" style="width:500px;"><span class="product-image-wrapper" style="padding-bottom: 100%;"><img class="product-image-photo" src="https://ddpzd2b97pj2g.cloudfront.net/pub/media/catalog/product/medium/VN0A4OOOBLK1_ComfycushPullover_1.jpg" alt="ComfyCush Pullover"></span></span> </a>  <div class="product details product-item-details" data-product-sku="VN0A4OOOBLK100L" data-product-modelno="VN0A4OOOBLK1" data-product-category="Apparels"><!-- start : Code for flags -->  <div class="flags_wrapper"><div class="flag">New Arrival</div></div><!-- end : Code for flags --> <strong class="product name product-item-name"><a class="product-item-link" href="https://www.vans.com.sg/comfycush-pullover-0a4ooo.html?color=3534">ComfyCush Pullover</a></strong>   <div class="price-box price-final_price" data-role="priceBox" data-product-id="" data-price-box="product-id-"><span class="price-container price-final_price tax weee"><span id="product-price-" data-price-amount="99.000000" data-price-type="finalPrice" class="price-wrapper "><span class="price">$99.00</span></span></span></div>  <div class="product-item-inner"><div class="product actions product-item-actions"> <input type="hidden" id="currentStoreId" value="5"><div id="btnGotowishlist-list" class="btn-add-to-wishlist"><a class="add-to-wishlist action towishlist log-in" href="javascript:void(0)" rel="https://www.vans.com.sg/ajaxwishlist/index/add" rev="" data-action="add-to-wishlist"></a> <a rel="" href="javascript:void(0)" class="action towishlist log-out hide"></a></div></div></div></div></div>   </li>


<span class="price">$99.00</span>

<a class="product-item-link" href="https://www.vans.com.sg/comfycush-pullover-0a4ooo.html?color=3534">ComfyCush Pullover</a>
</pre>

Looks like soup is unable to find anything and its returning None and Nonetype has no text property看起来汤找不到任何东西,它返回的 None 和 Nonetype 没有文本属性

The error looks here for soup in clothes: as you have overridden the soup parser错误在这里for soup in clothes:因为您已经覆盖了汤解析器


PS: PS:

You can use get_text() to extract the text您可以使用get_text()提取文本

price = soup.find('span', {'class' : 'price'}).get_text()

Sometimes you may have to use contents depending on the return type有时您可能必须根据返回类型使用contents

price = soup.find('span', {'class' : 'price'}).contents[0]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:AttributeError:&#39;NoneType&#39; 对象没有属性 &#39;text&#39; - Error: AttributeError: 'NoneType' object has no attribute 'text' AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;-Python,BeautifulSoup错误 - AttributeError: 'NoneType' object has no attribute 'text' - Python , BeautifulSoup Error python错误AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39; - python error AttributeError: 'NoneType' object has no attribute 'text' Python 错误消息 - AttributeError: 'NoneType' object 没有属性 'text' - Python Error Msg - AttributeError: 'NoneType' object has no attribute 'text' AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;-元组 - AttributeError: 'NoneType' object has no attribute 'text' - tuples AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;kivy - AttributeError: 'NoneType' object has no attribute 'text' kivy AttributeError:“ NoneType”对象在python中没有属性“ text” - AttributeError: 'NoneType' object has no attribute 'text' in python BeautifulSoup: AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39; - BeautifulSoup: AttributeError: 'NoneType' object has no attribute 'text' (Python) AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39; - (Python) AttributeError: 'NoneType' object has no attribute 'text' AttributeError: 'NoneType' object 没有属性 'text' - - AttributeError: 'NoneType' object has no attribute 'text' -
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM