其他分享
首页 > 其他分享> > 2020-03-04

2020-03-04

作者:互联网

利用tkinter库和爬虫技术构建一个简单的整合新闻资讯、天气查询、翻译服务的平台

import tkinter.messagebox
import requests,re,sys

SuperUser = ['admin123.']
users = []
def window0():
    print('\n\n在使用翻译服务时,须阅读《YTYG协议》。以下称为本协议。\n\n')
    print('1.在使用YTYG服务时,可能会有搜索错误或服务器崩溃的情况。')
    print('2.本程序数据来源于百度翻译、www.weather.com、凤凰资讯网。')
    print('3.未经过作者同意,不得私自转载、复制本程序,作者会保留上诉的权利。')
    print('4.此服务不对您在本服务中相关数据的删除或储存失败负责,您应对重要数据在本服务之外自行进行备份。\n')
    print('您在使用本服务时不得利用本服务从事以下行为,包括但不限于:')
    print('(1)传送、搜索、储存违反国家法律、危害国家安全统一、社会稳定、公序良俗、社会公德以及侮辱、诽谤、淫秽、暴力的内容;')
    print('(2) 故意频繁搜索低俗、色情、反动、暴力,或其他违背社会道德、危害国家安全的关键词。')
    print(
        '如果作者发现或收到他人举报您发布的信息违反本协议约定的,作者有权进行独立判断并采取技术手段予以删除、屏蔽或断开链接。同时,作者有权视用户的行为性质,采取包括但不限于限制、暂停或终止您使用本服务的全部或部分功能,限制、暂停或终止使用全部或部分功能,追究法律责任等措施。\n\n\n\n')
    print('\n\n\n\n警告⚠️:翻译服务只可用于英译中或中译英,且只能一词一词翻译。')
    print('错误的输入可能会引起系统报错。\n\n\n')
    print(
        'When using translation services, you must read the 《YTYG Agreement》. Hereinafter referred to as this agreement')
    print('1. When using the translation service, there may be search errors or server crashes.\n\n')
    print('2. The program data comes from Baidu translation、www.weather.com、fenghuang News')
    print(
        '3. This program may not be reproduced or copied without the consent of the author. The author reserves the right to appeal.')
    print(
        '4. This service is not responsible for your failure to delete or store relevant data in this service. You should back up important data yourself outside this service.\n')
    print(
        'When using this service, you shall not use this service to engage in the following activities, including but not limited to:')
    print(
        '(1) transmit, search, and store content that violates national laws, endangers national security, social stability, public order and good customs, social morality, and insults, defamation, obscenity, and violence;')
    print(
        '(2) Intentionally and frequently search vulgar, pornographic, reactionary, violent, or other keywords that violate social morals and endanger national security.')
    print(
        "If the author discovers or receives reports from others that your published information violates this agreement, the author has the right to make independent judgment and take technical measures to delete, block or break the link. At the same time, the author has the right to take measures including, but not limited to, restricting, suspending or terminating all or part of your use of the service, restricting, suspending or terminating all or part of the functions, and pursue legal liability, depending on the nature of the user's behavior\n\n\n\n")
    print('\n\n\nWARNING⚠️:Can only be used in English or Chinese to English translation, and can be translated word by word')
    print('Incorrect input may cause system error\n\n\n')
    print('\n\n 如有问题请联系作者:')
    print('邮箱:2158707744@qq.com\n\n')
    print('您是否同意本协议?\n')
    print('***********我同意-----1************')
    print('**********我不同意-----2***********')
    v = int(input('请输入:'))
    if v == 1:
        window1()
    elif v == 2:
        sys.exit(0)
    else:
        print('请正确输入')
        window0()

def window1():
    def window2change():
        myWindow1.destroy()
        window2()

    def mainpage():

        # 清理entry2
        entry2.delete(0, END)
        username = entry1.get()
        password = entry2.get()
        if [username, password] in users:
            tkinter.messagebox.showinfo('您已经成功登录!')
            myWindow1.destroy()
            window3()

        else:
            tkinter.messagebox.showinfo("WARNING",'错误1,mainpage(),17.请重试。')
            myWindow1.destroy()
            window1()

    def help():
        entry3.insert(0, '\n\n\n按确定键继续。')

    # 初始化Tk()
    myWindow1 = Tk()
    # 设置标题
    myWindow1.title('主页')
    # 标签控件布局
    Label(myWindow1, text="用户名").grid(row=0)
    Label(myWindow1, text="密码").grid(row=1)
    # Entry控件布局
    entry1 = Entry(myWindow1)
    entry2 = Entry(myWindow1, show='*')
    entry3 = Entry(myWindow1)
    entry1.grid(row=0, column=1)
    entry2.grid(row=1, column=1)
    entry3.grid(row=2, column=1)


    Button(myWindow1, text='退出', command=myWindow1.quit).grid(row=3, column=0, sticky=W, padx=5, pady=5)
    Button(myWindow1, text='注册', command=window2change).grid(row=3, column=1, sticky=W, padx=5, pady=5)
    Button(myWindow1, text='确定', command=mainpage).grid(row=3, column=2, sticky=W, padx=5, pady=5)
    Button(myWindow1, text="帮助", command=help).grid(row=2)

    myWindow1.mainloop()


def window2():
    def window1change():
        myWindow.destroy()
        window1()
    def register():
        entry2.delete(0, END)
        username = entry1.get()
        password = entry2.get()
        users.append([username,password])
        tkinter.messagebox.showinfo('您已经成功注册!')
        myWindow.destroy()
        window1()
    def help():
        entry3.insert(0,'\n\n\n按确定键继续。')
    # 初始化Tk()
    myWindow = Tk()
    # 设置标题
    myWindow.title('注册页面')

    # 标签控件布局
    Label(myWindow, text="用户名").grid(row=0)
    Label(myWindow, text="密码").grid(row=1)

    # Entry控件布局
    entry1 = Entry(myWindow)
    entry2 = Entry(myWindow,show = '*')
    entry3 = Entry(myWindow)
    entry1.grid(row=0, column=1)
    entry2.grid(row=1, column=1)
    entry3.grid(row=2, column=1)



    # Quit按钮退出;Run按钮打印计算结果
    Button(myWindow, text='退出', command=myWindow.quit).grid(row=3, column=0, sticky=W, padx=5, pady=5)
    Button(myWindow, text='返回登录',command=window1change).grid(row=3, column=1, sticky=W, padx=5, pady=5)
    Button(myWindow, text='确定', command=register).grid(row=3, column=2, sticky=W, padx=5, pady=5)
    Button(myWindow, text='帮助', command=help).grid(row=2,sticky=W, padx=5, pady=5)

    # 进入消息循环
    myWindow.mainloop()
def window3():
    def weather():
        myWindow.destroy()
        window4()
    # 初始化Tk()
    myWindow = Tk()
    # 设置标题
    myWindow.title('服务大厅')
    myWindow.geometry('1500x800')
    Button(myWindow, text='天气', height = 3, width = 50, command = window4).grid(row=0, column=100, sticky=N,  pady=60)
    Button(myWindow, text='新闻', height = 3, width = 50).grid(row=1, column=100, sticky=N,  pady=60)
    Button(myWindow, text='翻译', height = 3, width = 50,command = window5).grid(row=2, column=100, sticky=N,  pady=60)
    Button(myWindow, text='退出', command = myWindow.quit, height = 3, width = 50).grid(row=3, column=100, sticky=N, pady=60)
    myWindow.mainloop()



def window4():
    def back():
        myWindow.destroy()
        window3()
    def check():
        place = entry1.get()
        if place == '常德':
            cdweather()
        elif place == '郑州':
            zzweather()
        else:
            tkinter.messagebox.showinfo('⚠️','多次输入不正确可能会导致服务器崩溃。')
            myWindow.destroy()
            window4()



    def cdweather():
        response = requests.get(
            'https://weather.com/zh-CN/weather/today/l/29.04,111.69?par=apple_widget&locale=zh_CN1')
        response.encoding = 'utf-8'
        html = response.text
        reg = r'<span class="today-wx-descrip" id="dp0-details-narrative">(.*?)</span>'
        felttemp = re.findall(reg, html)
        felttemp1 = felttemp[0]
        entry2.insert(0,felttemp1)
    def zzweather():
        response = requests.get('https://weather.com/zh-CN/weather/today/l/CHXX0165:1:CH')
        response.encoding = 'utf-8'
        html = response.text
        reg = r'<span class="today-wx-descrip" id="dp0-details-narrative">(.*?)</span>'
        felttemp2 = re.findall(reg, html)
        entry2.insert(0,felttemp2[0])

    # 初始化Tk()
    myWindow = Tk()
    # 设置标题
    myWindow.title('天气查询')
    myWindow.geometry('1500x800')
    Label(myWindow, text="请输入您所在的城市",width=20,height=10).grid(row=0)
    Label(myWindow, text="查询结果",width=20,height=10).grid(row=1)
    # Entry控件布局
    entry1 = Entry(myWindow,width=70)
    entry2 = Entry(myWindow,width=70)


    entry1.grid(row=0, column=1)
    entry2.grid(row=1, column=1)


    Button(myWindow, text='查询', height=3, width=15, command=check).grid(row=0, column=100, sticky=N, pady=60,padx=20)
    Button(myWindow, text='返回', height=3, width=15, command=back).grid(row=1 , column=100, sticky=N, pady=60, padx=20)
    myWindow.mainloop()

def window5():
    def back():
        myWindow.destroy()
        window3()
    def check():
        words = entry1.get()
        translate3(words)
    def translate3(word):
        url = 'https://fanyi.baidu.com/sug'  ##请求url
        ##请求头
        headers = {
        'authority': 'fanyi.baidu.com',
        'method': 'POST',
        'accept': 'application/json, text/javascript, */*; q=0.01',
        'origin': 'https://fanyi.baidu.com',
        'Referer': 'https://fanyi.baidu.com/?aldtype=16047',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',}
        a = word
        data = 'kw=' + str(word)
        data = data.encode('utf-8')  ##将data字符串转化为utf-8
        js = requests.post(url=url, headers=headers, data=data).json()  ##获取到翻译结果
        Translate = js['data'][0]['v']  ##翻译词语的结果
        entry2.insert(0,Translate)
    # 初始化Tk()
    myWindow = Tk()
    # 设置标题
    myWindow.title('翻译服务')
    myWindow.geometry('1500x800')
    Label(myWindow, text="请输入需要查询的中文/英文", width=20, height=10).grid(row=0)
    Label(myWindow, text="查询目标英文/中文结果", width=20, height=10).grid(row=1)
    # Entry控件布局
    entry1 = Entry(myWindow, width=70)
    entry2 = Entry(myWindow, width=70)

    entry1.grid(row=0, column=1)
    entry2.grid(row=1, column=1)

    Button(myWindow, text='查询', height=3, width=15, command=check).grid(row=0, column=100, sticky=N, pady=60, padx=20)
    Button(myWindow, text='返回', height=3, width=15, command=back).grid(row=1, column=100, sticky=N, pady=60, padx=20)
window0()

标签:03,04,column,text,grid,print,2020,myWindow,row
来源: https://blog.csdn.net/weixin_45582247/article/details/104660314