AttributeError: 'NoneType' object has no attribute 'get_text'. BeautifulSoup objects failing

389 Views Asked by At

I'm webscraping a product in Amazon's website with BeautifulSoup, but when I run the code:

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

I get the error saying that the title and the price are nontype objects. How do I solve this problem?

1

There are 1 best solutions below

1
αԋɱҽԃ αмєяιcαη On
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/')

Output:

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