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

时间:2018-10-01 03:36:53

标签: python tkinter web-scraping scrapy

因此,我现在有一个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

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

1 个答案:

答案 0 :(得分:1)

尝试使用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)