【问题标题】:AttributeError: 'NoneType' object has no attribute 'get_text'. BeautifulSoup objects failingAttributeError:“NoneType”对象没有属性“get_text”。 BeautifulSoup 对象失败
【发布时间】:2021-08-13 00:45:39
【问题描述】:

我正在使用 BeautifulSoup 在亚马逊网站上网页抓取产品,但是当我运行代码时:

import requests
from bs4 import BeautifulSoup
import smtplib

URL = 'https://www.amazon.com.br/What-Wonderful-World-Inio-Asano/dp/6555941197/ref=sr_1_1? 
__mk_pt_BR=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=inio+asano&qid=1628810938&sr=8-1'

headers = {"user-agent":'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, 
like Gecko) Chrome/92.0.4515.131 Safari/537.36'}

def check_price():
    page = requests.get(URL, headers= headers)
    soup = BeautifulSoup(page.content, 'html.parser')

    title= soup.find(id="productTitle").get_text()
    price = soup.find(id ="price").get_text()
    converted_price = float(price[0:2])

我收到错误消息说标题和价格是非类型对象。我该如何解决这个问题?

【问题讨论】:

    标签: python web-scraping beautifulsoup data-science non-type


    【解决方案1】:
    import requests
    from bs4 import BeautifulSoup
    
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0'
    }
    
    
    def main(url):
        r = requests.get(url, headers=headers)
        soup = BeautifulSoup(r.text, 'lxml')
        title = " ".join([x.get_text(strip=True)
                         for x in soup.select('h1#title span')])
        price = soup.select_one(
            'span.a-size-base.a-color-price').get_text(strip=True).split()[-1]
        print(title, price)
    
    
    main('https://www.amazon.com.br/What-Wonderful-World-Inio-Asano/dp/6555941197/')
    

    输出:

    What A Wonderful World Capa comum – 20 junho 2021 47,90
    

    【讨论】:

    • 感谢您的合作,但在此之后我需要将价格变量转换为整数或布尔值,因为我的目标是制作一个机器人,当该漫画的价格下跌时向我发送电子邮件下来,所以在笔记本的最后它仍然告诉我一个无类型的对象没有属性'get_text()'
    猜你喜欢
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多