Python中的BS4代码格式和语法不正确--IDLE

时间:2019-05-26 02:52:00

标签: python

我最近完成了这一作业,并已完成所有语法和格式设置(将代码备份到云中的记事本中),并且在我的PC死机时丢失了所有内容。在使所有内容恢复正常运行时,需要比我更有经验的人帮助。

我已经尝试了运行调试软件的简短修复程序,但是在这一领域没有经验。

invoice_file = 'invoice.html'
from bs4 import BeautifulSoup 


watch = []

tshirts = []

cart = []

hats = []

global radiobtnClicked

global spinBox

# Code For UI

import tkinter as tk

def productList(event):
    global radiobtnClicked
    if event == 1:
        radiobtnClicked =1
        title = "Hats"
        page = download(url = 'http://www.westernhatstore.com/childrens-western-hats/',target_filename = 'hats',filename_extension = 'html')
        page_html = BeautifulSoup(page ,"html.parser" )
        page_content = page_html.find("ul",{"class":"ProductList"})
        page_contents = page_content.find_all("li")

        for page_content in page_contents:
            dataDict = dict()
            dataDict['Name'] = page_content.div.nextSibling.nextSibling.a.string
            dataDict['Image_Url'] = page_content.div.a.img['src']
            price_text = page_content.em.string
            price = price_text[1:]
            dataDict['Price'] = price
            hats.append(dataDict)

# print(hats)

# Creating New Window

newWindow = tk.Tk()

newWindow.geometry('400x550')

# Title

newWindow.title(title)

# Heading

lblNewWindowHeader = tk.Label(newWindow, text=title, bg="green" )

lblNewWindowHeader.grid(row = 0 ,column = 0 , columnspan = 3)

lblNewWindowHeader.config(font=('times',12, 'bold'))

lblNewWindowHeader.config(justify=tk.CENTER,width =44)

# Content

textContent = tk.Text(newWindow, height=31, width=49,relief="ridge",borderwidth=2 , bg="green")

textContent.grid(row = 1 ,column = 0, rowspan = 3)

# URL

lblURL = tk.Label(newWindow, text="URL", bg="green" )

lblURL.grid(row = 11,column = 0 , columnspan = 3)

lblURL.config(justify=tk.CENTER,width =57)

lblURL.config(text="http://www.westernhatstore.com/childrens-western-hats/")

# Populating Product

# print(hats[3]['Name'])

textContent.config(state = 'normal')

i = 0

for i in range(10):
    textContent.insert(tk.END,'#')
    textContent.insert(tk.END,i+1)
    textContent.insert(tk.END,': ')
    textContent.insert(tk.END,hats[i]['Name'])
    textContent.insert(tk.END,'(')
    textContent.insert(tk.END,hats[i]['Price'])
    textContent.insert(tk.END,')')
    textContent.insert(tk.END,'\n')

# i = i + 1

textContent.config(state = 'disabled')
if event == 1:
    radiobtnClicked = 1
elif event == 2:
    radiobtnClicked = 2

title = "Watch"

page = download(url = 'https://www.one-prices.com/',target_filename = 'watch',filename_extension = 'html')

page_html = BeautifulSoup(page ,"html.parser" )

page_contents = page_html.findAll("li",{"class":"index_pic"})

for page_content in page_contents:
    dataDict = dict()

dataDict['Name'] = page_content.nextSibling.a.string

img_url = page_content.a.img['src']

image_url = "https://www.one-prices.com/"+img_url

dataDict['Image_Url'] = image_url

price_text = page_content.nextSibling.nextSibling.string

price = price_text.split(" ")

dataDict['Price'] = price[2]

watch.append(dataDict)

# Creating New Window

newWindow = tk.Tk()

newWindow.geometry('400x550')

# Title

newWindow.title(title)

# Heading

lblNewWindowHeader = tk.Label(newWindow, text=title, bg="green" )

lblNewWindowHeader.grid(row = 0 ,column = 0 , columnspan = 3)

lblNewWindowHeader.config(font=('times',12, 'bold'))

lblNewWindowHeader.config(justify=tk.CENTER,width =44)

# Content

textContent = tk.Text(newWindow, height=31, width=49,relief="ridge",borderwidth=2 , bg="green")

textContent.grid(row = 1 ,column = 0, rowspan = 3)

# URL

lblURL = tk.Label(newWindow, text="URL", bg="green" )

lblURL.grid(row = 11,column = 0 , columnspan = 3)

lblURL.config(justify=tk.CENTER,width =57)

lblURL.config(text="https://www.one-prices.com/")

textContent.config(state = 'normal')

i = 0

for i in range(10):
    textContent.insert(tk.END,'#')

    textContent.insert(tk.END,i+1)

    textContent.insert(tk.END,': ')

    textContent.insert(tk.END,watch[i]['Name'])

    textContent.insert(tk.END,'(')

    textContent.insert(tk.END,watch[i]['Price'])

    textContent.insert(tk.END,')')

    textContent.insert(tk.END,'\n')

# i = i + 1

textContent.config(state = 'disabled')
if event == 1:
    radiobtnClicked = 1
elif event == 2:
    radiobtnClicked = 2
elif event == 3:
    radiobtnClicked = 3

title = "T-Shirts"

page = download(url = 'https://www.thinkgeek.com/clothing/t-shirts/',target_filename = 'tshirts',filename_extension = 'html')

page_html = BeautifulSoup(page ,"html.parser" )

page_contents = page_html.findAll("div",{"class":"product"})

for page_content in page_contents:
    dataDict = dict()

dataDict['Name'] = page_content.a.h4.string

img_url = page_content.a.img['src']

image_url = "https://www.thinkgeek.com"+img_url

dataDict['Image_Url'] = image_url

price_text = page_content.p.contents[0].strip()

price = price_text[1:]

dataDict['Price'] = price

tshirts.append(dataDict)

# Creating New Window

newWindow = tk.Tk()

newWindow.geometry('400x550')

# Title

newWindow.title(title)

# Heading

lblNewWindowHeader = tk.Label(newWindow, text=title, bg="green" )

lblNewWindowHeader.grid(row = 0 ,column = 0 , columnspan = 3)

lblNewWindowHeader.config(font=('times',12, 'bold'))

lblNewWindowHeader.config(justify=tk.CENTER,width =44)

# Content

textContent = tk.Text(newWindow, height=31, width=49,relief="ridge",borderwidth=2 , bg="green")

textContent.grid(row = 1 ,column = 0, rowspan = 3)

# URL

lblURL = tk.Label(newWindow, text="URL", bg="green" )

lblURL.grid(row = 11,column = 0 , columnspan = 3)

lblURL.config(justify=tk.CENTER,width =57)

lblURL.config(text="https://www.thinkgeek.com/clothing/t-shirts/")

textContent.config(state = 'normal')

i = 0

for i in range(10):
    textContent.insert(tk.END,'#')

    textContent.insert(tk.END,i+1)

    textContent.insert(tk.END,': ')

    textContent.insert(tk.END,tshirts[i]['Name'])

    textContent.insert(tk.END,'(')

    textContent.insert(tk.END,tshirts[i]['Price'])

    textContent.insert(tk.END,')')

    textContent.insert(tk.END,'\n')

# i = i + 1

textContent.config(state = 'disabled')
if event == 1:
    radiobtnClicked = 1
elif event == 2:
    radiobtnClicked = 2
elif event == 3:
    radiobtnClicked = 3   
elif event== 4:
    radiobtnClicked =4

print ("hello")

newWindow.mainloop()



# main Function

def main():
    global spinBox

# Creating the window

root = tk.Tk()

root.geometry('500x320')

var = tk.IntVar()

# Title

root.title('Abay shopping App')

root.configure(background="#b20833")

# Header Text

lblHeader = tk.Label(root, text='The Power of All of Us' , bg="#b20833")

lblHeader.grid(row=0, column=0,columnspan =4)

lblHeader.config(font=('times',15, 'bold'))

lblHeader.config(justify=tk.CENTER,width=44)

# Logo

canvasLogo = Canvas(root, width = 300, height = 280,bg="#b20833")

canvasLogo.grid(row=1, column=0,rowspan=4, columnspan= 2)

# Logo Image

logo = PhotoImage(file="logo.png")

canvasLogo.create_image(10,10, anchor=NW, image=logo)

# Radio Button Group One

lblframeOne = LabelFrame(root, text="Bargain Bin ",bg="#b20833")

lblframeOne.grid(row=1, column=2,columnspan= 2)

radioBtnOne = Radiobutton(lblframeOne, text="Hats ",variable=var, value=1 ,command=lambda *args: productList(1),bg="#b20833")

radioBtnOne.grid(row=0, column=0)

radioBtnTwo = Radiobutton(lblframeOne, text="Watch ", variable=var, value=2 ,command=lambda *args: productList(2),bg="#b20833")

radioBtnTwo.grid(row=0, column=1)

# Radio Button Group Two

lblframeTwo = LabelFrame(root, text="Today's Special",bg="#b20833")

lblframeTwo.grid(row=2, column=2,columnspan= 2)

radioBtnThree = Radiobutton(lblframeTwo, text="T-Shirts", variable=var, value=3,command=lambda *args: productList(3),bg="#b20833")

radioBtnThree.grid(row=0, column=0)

radioBtnFour = Radiobutton(lblframeTwo, text="Option 4", variable=var, value=4 ,command=lambda *args: productList(4),bg="#b20833")

radioBtnFour.grid(row=0, column=1)

# Label for

lblItemNumberlbl = tk.Label(root, text="Item Number",bg="#b20833")

lblItemNumberlbl.grid(row=3, column=2)

# Spinbox

spinBox = Spinbox(root, from_=0, to=10,bg="#b20833")

# lblItemNumber = tk.Label(root, text="###")

spinBox.grid(row=3, column=3)

# Add to Cart Button

btnAddToCart = tk.Button(root, text="Add To Cart" , command=addToCart)

btnAddToCart.grid(row=4, column=2)

# Print Invoice Button

btnPrintInvoice = tk.Button(root, text="Print Invoice" ,command=printInvoice)

btnPrintInvoice.grid(row=4, column=3)

# lblLoanAmount = tk.Label(root, text="Loan Amount").grid(row=3, column=0)

# lblMonthlyPay = tk.Label(root, text="Monthly Payment").grid(row=4, column=0)

# lblTotalPay = tk.Label(root, text="Total Payment").grid(row=5, column=0)

#pack(fill="both", expand="yes")

# left = Label(lblframeOne, text="Inside the LabelFrame")

# left.pack()

root.mainloop()

def addToCart():
    if radiobtnClicked == 1:
        dataDict = dict()
choice = int(spinBox.get())

dataDict['Name'] = hats[choice]['Name']

dataDict['Image_Url']= hats[choice]['Image_Url']

dataDict['Price'] = hats[choice]['Price']

cart.append(dataDict)
if radiobtnClicked == 1:
        dataDict = dict()
elif radiobtnClicked == 2:
    dataDict = dict()

choice = int(spinBox.get())

dataDict['Name'] = hats[choice]['Name']

dataDict['Image_Url']= hats[choice]['Image_Url']

dataDict['Price'] = hats[choice]['Price']

cart.append(dataDict)
if radiobtnClicked == 1:
        dataDict = dict()
elif radiobtnClicked == 2:
    dataDict = dict()
elif radiobtnClicked == 3:
    dataDict = dict()

choice = int(spinBox.get())

dataDict['Name'] = hats[choice]['Name']

dataDict['Image_Url']= hats[choice]['Image_Url']

dataDict['Price'] = hats[choice]['Price']

cart.append(dataDict)
if radiobtnClicked == 1:
        dataDict = dict()
elif radiobtnClicked == 2:
    dataDict = dict()
elif radiobtnClicked == 3:
    dataDict = dict()
elif radiobtnClicked == 4:
    print (radiobtnClicked)

def printInvoice():
    total_bill=0

invoice_products = ""

for x in cart:
    total_bill = total_bill + float(x['Price'])

    invoice_header ="<!DOCTYPE html><html><head><title>Abay shopping App Invoice</title></head><body>"

    invoice_heading ="<center><b><h1>Abay shopping App Invoice</h1</b>>"

    invoice_logo = "<img src='logo.png'>"

    invoice_subheading = "<b><h2>Total for the purchases below :"+str(total_bill)+"</h2>AUD<p>(Prompt payment is appreciated!)</p</b>"

for x in cart:
    invoice_products = invoice_products + "<b><h2>"+ x['Name']+"</h2></b><img src='"+x['Image_Url'] +"'><p>Our Price: $"+x['Price'] +"USD</p>"

    invoice_footer= "</center></body></html>"

    invoice = invoice_header + invoice_heading+ invoide_logo +invoice_subheading + invoice_products +invoice_footer

f = open(invoice_file,'w')

f.write(invoice)

f.close()

# Calling the main function

if __name__== "__main__":
    main()

0 个答案:

没有答案