简体   繁体   English

python中的网页抓取和GUI。 应用程序冻结

[英]Webscraping and GUI in python. App freezing

Hi I am getting trouble running my app.嗨,我在运行我的应用程序时遇到问题。 I am new to coding so any advice on my code could would be appreciated.我是编码新手,因此对我的代码的任何建议都将不胜感激。 I am trying to make a GUI for my webscraper.我正在尝试为我的 webscraper 制作一个 GUI。 Without the GUI it works perfectly fine and scrapes all the info into a .csv file.如果没有 GUI,它可以完美运行并将所有信息刮到一个 .csv 文件中。 Then when i created the GUI with buttons to make it run, it freezes.然后,当我使用按钮创建 GUI 以使其运行时,它会冻结。 I tried changing the sleep to root.after o even deleting that row because I read in other threads that it is the main reason for the GUI to freeze, but still couldn't get it fixed.我尝试将 sleep 更改为 root.after o 甚至删除该行,因为我在其他线程中读到这是 GUI 冻结的主要原因,但仍然无法修复它。

Also, on the functions I defined, im not sure if it is ok to put any random word like "prices" because I am not using it or what would be the correct way.另外,在我定义的函数上,我不确定是否可以放置任何像“价格”这样的随机词,因为我没有使用它或者什么是正确的方法。

Here is my code, thanks in advance.这是我的代码,提前致谢。

import requests
from bs4 import BeautifulSoup
from time import sleep
from random import randint
import csv
import os
from tkinter import *
from tkinter import ttk

root = Tk()
root.title("Mercados App")
root.geometry("500x500")
app = Frame(root)


def updating(prices):

switch = True
if switch:
    f = open("ListaDePreciosTodos.csv", "wt", newline="")
    writer = csv.writer(f)

    file_lista_productosPV = open("ListaPV", "r")
    lista_productosPV = file_lista_productosPV.readlines()

    file_lista_productosT = open("ListaT", "r")
    lista_productosT = file_lista_productosT.readlines()

    file_lista_productosW = open("ListaW", "r")
    lista_productosW = file_lista_productosW.readlines()
    writer.writerow(["PV Producto", "PV Precio", "T Producto", "T Precio",
                     "W Producto", "W Precio"])

    os.system('say "Price updates starting now"')

    for searchPV, searchT, searchW in zip(lista_productosPV, lista_productosT, lista_productosW):
        urlPV = "https://www.pv.com/" + searchPV.strip() + "/p"
        rPV = requests.get(urlPV)
        soupPV = BeautifulSoup(rPV.content, "lxml")
        urlT = "http://www.T.com.pe/t/product/" + searchT.strip() + "?navAction=jump&navCount=2"
        rT = requests.get(urlT)
        soupT = BeautifulSoup(rT.content, "lxml")
        urlW = "https://www.w.com/" + searchW.strip() + "/p"
        rW = requests.get(urlW)
        soupW = BeautifulSoup(rW.content, "lxml")

        try:
            productoPV = soupPV.find(["div"], {"class": ["g-nombre-prod"]}).text
            precioPV = soupPV.find(["strong"], {"class": ["skuBestPrice"]}).text

            productoT = soupT.find(["div"], {"class": ["title"]}).h5.text
            precioTxKG = soupT.find(["div"], {"class": ["price-unit"]}).text
            precioTxUN = soupT.find(["span"], {"class": ["active-price"]}).span.text

            if precioTxKG.strip() is "":
                precio_final_t = precioTxUN.replace("S/ ", "").strip()

            else:
                precio_final_t = precioTxKG.replace("/KG)","").replace("(","").replace("S/ ","").strip()

            productoW = soupW.find(["div"], {"class": ["name"]}).text
            precioW = soupW.find(["strong"], {"class": ["skuBestPrice"]}).text


            writer.writerow([productoPV, precioPV.replace("S/", ""),
                             productoT.replace("VERDURAS" or "FRUTAS" or "T", "").strip(),
                             precio_final_t, productoW, precioW.replace("S/. ", "")])

        except AttributeError:

            try:
                writer.writerow([productoPV, "No Disp.",
                                productoT.replace("VERDURAS" or "FRUTAS" or "T", "").strip(),
                                precio_final_t, productoW, precioW.replace("S/. ", ""), ])

            except AttributeError:

                try:
                    writer.writerow([productoPV, precioPV.replace("S/", ""),
                                    productoT.replace("VERDURAS" or "FRUTAS" or "T", "").strip(),
                                    "No Disp.", productoW, precioW.replace("S/. ", ""), ])

                except:
                    writer.writerow([productoPV, precioPV.replace("S/", ""),
                                     productoT.replace("VERDURAS" or "FRUTAS" or "T", "").strip(),
                                     precio_final_t, productoW, "No Disp."])

    f.close()
    os.system('say "your file is ready"')


def stopupdating(prices):
    global switch
    switch = False


app.grid()

Label(root, text="Bienvenidos").grid(row=0, column=1, sticky=W, padx=8)

startButton = Button(root, text="Start updating prices")
startButton.bind("<Button-1>", updating)
startButton.grid(row=1, column=1, sticky=W, padx=10)

stopButton = Button(root, text="Stop updating prices")
stopButton.bind("<Button-1>", stopupdating)
stopButton.grid(row=2, column=1, sticky=W, padx=10)

root.mainloop()

In your code, create a new method:在您的代码中,创建一个新方法:

def start_update():
    threading.Thread(target=updating).start()

You will need to add import threading also.您还需要添加import threading

And bind your button to this method instead of directly to updating .并将您的按钮绑定到此方法,而不是直接绑定到updating I see your updating method has an argument of prices , but I don't see how that is assigned in your code or where it is even used.我看到您的updating方法有一个prices参数,但我看不到您的代码中是如何分配的,或者甚至在哪里使用它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM