繁体   English   中英

如何使用Tkinter python GUI运行我的Webscraper?

[英]How to use Tkinter python GUI to RUN my webscraper?

因此,我现在有了一个Python网络爬虫,当它运行时,会提示用户选择1、2、3或全部。 这些选项会根据编号刮取网站。 我想制作一个python gui,当按下按钮(或复选框)时,它们将运行与1、2、3或全部相关的功能! 这是我的一些代码,所以您知道我从哪里开始:

from tkinter import * #import statement for dependencies

master = Tk()
var1 = IntVar()
Checkbutton(master, text='Scraper 1', variable=var1).grid(row=0, sticky=W)
var2 = IntVar()
Checkbutton(master, text='Scraper 2', variable=var2).grid(row=1, sticky=W)
var3 = IntVar()
Checkbutton(master, text='Scraper 3', variable=var3).grid(row=2, sticky=W)
mainloop()

这是我的一些scraper代码,因此您可以看到选项1、2、3或全部的含义:

import os, re, scrapy, sys, subprocess, xlwt
from tempfile import TemporaryFile


question2 = input("Which sites? 1 Grainger, 2 instrumart, 3 TruTechTools, 4 FWWebb, or All (1/2/3/All)")
if(question2 == "1"):
    scraper = 1

elif(question2 == "2"):
    scraper = 2
elif(question2 == "3"):
    scraper = 3
elif(question2 == "4"):
    scraper = 4
else:
    scraper = "all"
if (scraper == 1):
    subprocess.call('scrapy runspider graingerScraper.py -o info.json')
    fo = open("info.json", "r")#opens the file for our program to reference. Setting fo to the file, and as a read file variable
    print ("Name of the file: ", fo.name)#we don't need this, its just nice for our output rn
    line = fo.readlines()#creating a String variable = our names.json file

请帮助我了解如何将按钮链接到运行实际的刮板代码!

尝试使用PySimpleGUI作为GUI框架。 听起来您只需要显示一些按钮,按下这些按钮,然后调用某些功能即可。 使用PySimpleGUI可能需要10行代码。 从菜谱中复制一些代码并运行它,您将立即了解如何使用它。

这段代码可能会让您入门。 它产生以下GUI: https : //user-images.githubusercontent.com/13696193/46325549-c3902380-c5c6-11e8-9e8a-75ba89c99bc5.jpg

import PySimpleGUI as sg

layout = [[ sg.Text('My Web Scraper') ],
          [ sg.Button('Grainger'), sg.Button('instrumart'), sg.Button('TruTechTools')],
          ]

window = sg.Window('My window').Layout(layout)
button, value = window.Read()

if button == 'Grainger':
    scraper = 1
elif button == 'instrumart':
    scraper = 2
elif button == 'TruTechTools':
    scraper = 3
print(scraper)

暂无
暂无

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

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