简体   繁体   中英

Don't understand why an IndentationError occurs in this code that converts int to float

I keep getting the IndentationError: unindent does not match any outer indentation level error when trying to build an HTML scraper that can look for discounts in price. But when converting int to float .

converted_price = float(price[0:5])
if(converted_price < 40.99):
        send_mail()
    print(converted_price)
    print(title.strip())
    if(converted_price > 40.99):
        send_mail() 

I get the error message. What am I doing wrong?

Full code:

import requests
from bs4 import BeautifulSoup
import smtplib

URL = 'https://www.amazon.de/Toilettendeckel-Absenkautomatik-Antibakterieller-Urea-Duroplast-Edelstahlscharnier/dp/B0881PKQ2H/?_encoding=UTF8&smid=AKQL6N75FLK4O&pd_rd_w=hTIPC&pf_rd_p=d051a36d-9331-41c8-9203-e7d634b1ee23&pf_rd_r=3TS01EKWNMYSRC1147X1&pd_rd_r=d950f9b1-8e9a-4913-b266-9b7a36ad21f5&pd_rd_wg=GLsoO&ref_=pd_gw_unk'

headers = {"User-agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 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="priceblock_saleprice").get_text
    converted_price = float(price[0:5])

if(converted_price < 40.99):
        send_mail()

    print(converted_price)
    print(title.strip())

    if(converted_price > 40.99):
        send_mail()


def send_mail():
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()

    server.login('madbro88a@gmail.com', 'gsplmdqkaavnuxnb')

    subject = 'Price fell down!'
    body = 'https://www.amazon.de/Toilettendeckel-Absenkautomatik-Antibakterieller-Urea-Duroplast-Edelstahlscharnier/dp/B0881PKQ2H/?_encoding=UTF8&smid=AKQL6N75FLK4O&pd_rd_w=hTIPC&pf_rd_p=d051a36d-9331-41c8-9203-e7d634b1ee23&pf_rd_r=3TS01EKWNMYSRC1147X1&pd_rd_r=d950f9b1-8e9a-4913-b266-9b7a36ad21f5&pd_rd_wg=GLsoO&ref_=pd_gw_unk'

    msg = f"Subject: {subject}\n\n{body}"

    server.sendmail(
        'mail1',
        'mail2',
        msg
    )
    print('Email has been sent!')

    server.quit()


price_check()

The source of the error is in this section:

if(converted_price < 40.99):
        send_mail()

In Python after any line that requires an indentation such as an if , while , def , etc you must indent the following line by one tab, or the equivalent using spaces. at least one space. All subsequent lines in an indented block should be indented to the same level. Convention is to use 4 spaces (thanks mkrieger1 ). Whitespace is very important in Python.

In your example you have indented the line send_mail() two tabs deeper than the if statement. If you reduce the indentation of send_mail() by one the compiler error is resolved.

The corrected code should be:

if(converted_price < 40.99):
    send_mail()

Note that in your full code you have the following block indented one too little:

if(converted_price < 40.99):
        send_mail()

    print(converted_price)
    print(title.strip())

    if(converted_price > 40.99):
        send_mail()

This whole block needs to be indented to be within the def check_price(): function.

in Full code if(converted_price < 40.99): is part of the check_price() function, so you have to indent it.

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