Python 垃圾分类查询

基于网站  https://lajifenleiapp.com/ 做的二次开发,通过request传递参数post并获取result

源码如下:

import requests
from bs4 import BeautifulSoup
from urllib.request import quote
import re
import tkinter as tk
window = tk.Tk()
window.title('垃圾分类查询')
window.geometry('500x300')  # 这里的乘是小x

def check_Action():
    s_garbage = var_garbage.get()    
    # 转码
    sGoods = quote(s_garbage,encoding='utf-8')
    try:
        r = requests.get('https://lajifenleiapp.com/sk/' + sGoods)
        # 获取数据
        bs_ele= BeautifulSoup(r.text,'html.parser')
        bs_all_row = bs_ele.find_all('div', class_ = 'row')
        bs_row = bs_all_row[2]
        bs_span = bs_row.find_all('span')
        # 鱼骨
        #print(bs_span[0].text)
        # 属于
        match_obj = re.findall(r'[\u4e00-\u9fa5]+',bs_span[1].text)
        #print(match_obj[0])
        # 湿垃圾(厨余垃圾)
        #print(bs_span[2].text)    
        s_what.set(bs_span[0].text)
        s_belog.set(match_obj[0])
        s_target.set(bs_span[2].text)        
    except Exception:
        s_what.set(s_garbage)
        s_belog.set('不属于')
        s_target.set('任何垃圾')          


tk.Label(window, text='垃圾名:', font=('Calibri', 14)).place(x=20, y=20)

var_garbage = tk.StringVar()
var_garbage.set('查询垃圾')
var_garbage = tk.Entry(window, textvariable=var_garbage, font=('Calibri', 14),width = 15)
var_garbage.place(x=100,y=20)

btn_query = tk.Button(window, text='查询', font=('Calibri', 11),command=check_Action)
btn_query.place(x=260, y=17)

s_what = tk.StringVar()
l_what = tk.Label(window, bg='yellow', fg='green',font=('Calibri', 12), width=15, textvariable=s_what)
l_what.place(x=20,y=60)

s_belog = tk.StringVar()
l_belog = tk.Label(window, bg='red', fg='green',font=('Calibri', 12), width=10, textvariable=s_belog)
l_belog.place(x=160,y=60)

s_target = tk.StringVar()
l_target = tk.Label(window, bg='black', fg='yellow',font=('Calibri', 12), width=15, textvariable=s_target)
l_target.place(x=260,y=60)

window.mainloop()





也有程序快捷下载方式:

https://download.csdn.net/download/q215046120/11412840

你可能感兴趣的:(Python)