# 这里必须用sys新建模块路径,不然下面的模块会导入不了 |
# import sys |
# sys.path.append(r'C:\Users\zhubiping\appdata\roaming\tc\python-3.6.4\lib\site-packages') |
''' |
提示: |
1.开头的登录直接在用户名那里输入‘1’就可以进去了 |
2.有一个叫‘Nominatim’的模块方法可能会没有,需要导入(其他模块也是一样) |
''' |
import random |
import re |
import os |
import time |
import easygui as e |
import tkinter as tk |
from tkinter import messagebox,filedialog |
from geopy.geocoders import Nominatim |
import string |
import socket |
# 用于获取网页内容的模块和解析网页源代码的模块 |
import requests |
from bs4 import BeautifulSoup |
from urllib.parse import urlencode |
import urllib.parse |
names = [ 'mikekil' ] |
passwords = [ '123456' ] |
title = '您的智能助手' |
#Errors |
cityNotFoundError = "错误:此城市不属于中国之内。\n或者系统找不到此地址,可另换输入内容或点击‘当前地址’获取" |
root = tk.Tk() |
root.withdraw() |
#===============================工具方法===================================# |
def is_all_chinese(strs): #判断是否是纯中文 |
for i in strs: |
if not '\u4e00' < = i < = '\u9fa5' : |
return False |
return True |
|
def is_all_eng(strs): #判断是否是纯英文 |
for i in strs: |
if i not in string.ascii_lowercase + string.ascii_uppercase: |
return False |
return True |
#===============================工具方法===================================# |
class Getoutofloop(Exception): |
pass |
def get_host_ip(): |
""" |
查询本机ip地址 |
:return: |
""" |
try : |
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) |
s.connect(( '8.8.8.8' , 80 )) |
ip = s.getsockname()[ 0 ] |
finally : |
s.close() |
return ip |
def SignIn(): |
isRun = True |
content = e.multpasswordbox( "请先输入用户名或密码:" ,title,( "用户名:" , "密码:" ),()) |
while isRun: |
if content = = None : |
exit( 0 ) |
elif content[ 0 ] = = "1" : |
isRun = False |
break |
if content[ 0 ] in names: |
for i in range ( len (names)): |
if names[i] = = content[ 0 ] and passwords[i] = = content[ 1 ]: |
e.msgbox( "-登录成功-" ) |
isRun = False |
break |
else : |
content = e.multpasswordbox( "(密码错误!)\n请先输入用户名或密码:" ,title,( "用户名:" , "密码:" ),()) |
continue |
break |
else : |
e.msgbox( "-该用户不存在-" ) |
content = e.multpasswordbox( "请先输入用户名或密码:" ,title,( "用户名:" , "密码:" ),()) |
def delete_Musics(): |
paths = filedialog.askopenfilenames(title = "choose a Music" , |
initialdir = "C:/" , |
filetypes = [( "MP3" , "mp3" )]) |
for path in paths: |
os.remove(path) |
|
def getWeatherData(cityName): |
url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + cityName |
response = requests.get(url) |
weatherDict = response.json() |
print (weatherDict) |
if weatherDict[ 'desc' ] = = 'OK' : |
# 获取城市名 |
global city, wendu,month,forecast |
city = weatherDict[ 'data' ][ 'city' ] |
# 获取当前温度 |
wendu = weatherDict[ 'data' ][ 'wendu' ] + '℃ ' |
month = time.strftime( '%m' ) |
forecast = weatherDict[ 'data' ][ 'forecast' ] |
# 获取天气类型 |
type = forecast[ 0 ][ 'type' ] |
# 获取最高温度 |
high = forecast[ 0 ][ 'high' ] |
# 获取最低温度 |
low = forecast[ 0 ][ 'low' ] |
tip = weatherDict[ 'data' ][ 'ganmao' ] |
content = '城市:' + city + '\n当前温度:' + wendu + '\n天气:' + type + '\n最高温度:' + high + '\n最低温度:' + low + '\n注意:' + tip |
else : |
content = '-您的网络不佳-' |
return content |
|
def WeatherGet(): |
cityNames = [ '北京' , '上海' , '广州' , '深圳' , '哈尔滨' , '辽宁' , '新疆' , '海南' , '重庆' , |
'武汉' , '浙江' , '杭州' , '云南' , '桂林' , '南宁' ] |
mode = e.buttonbox( "请选择城市:" ,title,[ '当前地址' , '选择地址' , '输入地址' , '返回' ]) |
if mode = = "选择地址" : |
city = e.choicebox( "请选择城市:" ,title,cityNames) |
elif mode = = "输入地址" : |
cityPos = e.enterbox( "请输入城市:" ,title) |
if '路' or '街' or '区' or '镇' in cityPos: |
try : |
geolocator = Nominatim(user_agent = "aaa515" ) |
location = geolocator.geocode(cityPos) |
pos = location.address |
country = pos.split( ', ' )[:: - 1 ][ 0 ] |
city = pos.split( ', ' )[:: - 1 ][ 3 ] |
if country ! = '中国' : |
e.msgbox(cityNotFoundError,title) |
return |
except : |
e.exceptionbox( "系统没有找到您的位置,以下是系统报错信息,快上报给作者吧!" , "Error404 系统出错啦!" ) |
return |
else : |
city = cityPos |
elif mode = = "返回" : |
return |
else : |
messagebox.showinfo( "提示" , "‘当前地址’功能尚未完善,快去试试其他功能吧" ) |
return |
# cityIP = get_host_ip() |
# print(cityIP) |
# city = cityIP |
e.textbox( "结果如下:" ,title,getWeatherData(city)) |
start_Ask() |
def Get_BaiduSearch(): |
okBths = [ '好的' , '嗯' ] |
image = r "C:\Users\zhubiping\Desktop\MIKE_FILES\images\ordinary普通图片\wotomalen.gif" |
content = e.enterbox( "输入关键名词(直接回车可以退出):" ,title) |
if content = = None or content = = "": |
return |
try : |
# 分解问题(转换格式) |
q = urllib.parse.quote(content) |
# URL百度搜索地址 |
url = 'https://baike.baidu.com/item/' + q |
# 获取百度网页内容 |
html = urllib.request.urlopen(url) |
# 读取百度网页内容 |
content = html.read().decode( 'utf-8' ) |
html.close() |
# 解析代码 |
soup = BeautifulSoup(content, "lxml" ) |
# 抽取内容 |
text = soup.find( 'div' , class_ = "lemma-summary" ).children |
result = '' |
# 获取内容 |
for x in text: |
word = re.sub(re. compile (r "<(.+?)>" ),'', str (x)) |
result + = str (re.sub(re. compile (r "\[(.+?)\]" ),'',word)) |
# 显示 |
e.textbox( '结果如下:' ,title,result) |
except : |
e.msgbox( "我的脑子有点乱>﹏<。" ,title,okBths[ 0 ]) |
start_Ask() |
|
def IntelligentTools(): |
toolsList = [ '返回' , '文字识别' , '车型识别' , '图片特效' , '图片上色' , "人物动漫化" ] |
modeTop = e.choicebox( '请选择您需要打开的智能工具(此功能需要百度AI还有其他文件,如有需要请告知作者)' ,title,choices = toolsList) |
if modeTop = = toolsList[ 0 ]: |
return |
elif modeTop = = toolsList[ 1 ]: |
#os.system("python index.py") |
messagebox.showinfo( "提示" , "‘文字识别’功能尚未完善,快去试试其他功能吧" ) |
elif modeTop = = toolsList[ 2 ]: |
messagebox.showinfo( "提示" , "‘车型识别’功能尚未完善,快去试试其他功能吧" ) |
#os.system("python VehicleIdentification.py") |
elif modeTop = = toolsList[ 3 ]: |
messagebox.showinfo( "提示" , "‘图片特效’功能尚未完善,快去试试其他功能吧" ) |
#os.system("python PictureEffects.py") |
elif modeTop = = toolsList[ 4 ]: |
messagebox.showinfo( "提示" , "‘图片上色’功能尚未完善,快去试试其他功能吧" ) |
#os.system("python PictureColoring.py") |
elif modeTop = = toolsList[ 5 ]: |
messagebox.showinfo( "提示" , "‘人物动漫化’功能尚未完善,快去试试其他功能吧" ) |
#os.system("python -.py") |
def Musics_DownLoad(): |
header = { # 伪造浏览器头部,不然获取不到网易云音乐的页面源代码。 |
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36' , |
} |
topList = [ '飙升榜' , '新歌榜' , '原创榜' , '热歌榜' , '返回' ] |
songTop = e.choicebox( '选择下载的歌榜' , 'Music Download' ,choices = topList) |
# 这是网易云音乐热歌榜的链接(其实是嵌套在网页里面含有歌曲数据的页面框架的真实链接) |
link1 = "https://music.163.com/discover/toplist?id=3778678" |
link2 = "https://music.163.com/discover/toplist?id=19723756" |
link3 = "https://music.163.com/discover/toplist?id=2884035" |
link4 = "https://music.163.com/discover/toplist?id=3779629" |
if songTop = = "" or songTop = = None : |
songTop = e.choicebox( '选择下载的歌榜' , 'Music Download' ,choices = topList) |
elif songTop = = topList[ 0 ]: link = link1 |
elif songTop = = topList[ 1 ]: link = link2 |
elif songTop = = topList[ 2 ]: link = link3 |
elif songTop = = topList[ 3 ]: link = link4 |
else : return |
# 通过 requests 模块的 get 方法获取网页数据 |
r = requests.get(link, headers = header) |
# 获取网页内容 |
html = r.content |
# 通过 BeautifulSoup 模块解析网页,具体请参考官方文档。 |
soup = BeautifulSoup(html, "html.parser" ) |
# 通过分析网页源代码发现排行榜中的歌曲信息全部放在类名称为 f-hide 的 ul 中, |
# 于是根据特殊的类名称查找相应 ul,然后找到里面的全部 a 标签, |
# 限制数量为200,即排行榜的前 200 首歌。 |
songs = soup.find( "ul" , class_ = "f-hide" ).select( "a" , limit = 200 ) |
root = tk.Tk() |
root.withdraw() |
|
namelist = [] |
linklist = [] |
for s in songs: # 遍历输出数组 songs 中的内容 |
song_id = s[ 'href' ][ 9 :] # 只截取歌曲链接中的 ID 部分,因为网页中链接的形式为“/song?id=496870798”,从 = 号之后的就是歌曲的 ID 号。 |
name = s.text # 获取 a 标签的文本内容,即歌曲的名称。 |
namelist.append(name) |
song_down_link = "http://music.163.com/song/media/outer/url?id=" + song_id + ".mp3" |
linklist.append(song_down_link) |
print (namelist) |
|
song_name = e.choicebox( '选择下载的歌曲' , 'Music Download' ,choices = namelist) |
|
for i in range ( len (namelist)): |
if namelist[i] = = song_name: |
song_down_link = linklist[i] |
break |
|
response = requests.get(song_down_link, headers = header).content |
path = filedialog.askdirectory() |
# print(song_name) |
with open (path + "/" + song_name + ".mp3" , 'ab+' ) as f: |
f.write(response) |
|
messagebox.showinfo( "提示" , "下载完成" ) |
start_Ask() |
def start_Ask(): |
global action |
list = [ 'EXIT' , '百度搜索' , '音乐下载' , "删除音乐文件" , "天气查询" , "智慧识别" ] |
action = e.buttonbox( "请选择您现在要做的操作:" , "智能助手" , list ) |
while action ! = list [ 0 ]: |
if action = = list [ 0 ]: |
break |
elif action = = list [ 1 ]: |
Get_BaiduSearch() |
action = e.buttonbox( "请选择您现在要做的操作:" , "智能助手" , list ) |
elif action = = list [ 2 ]: |
Musics_DownLoad() |
action = e.buttonbox( "请选择您现在要做的操作:" , "智能助手" , list ) |
elif action = = list [ 3 ]: |
delete_Musics() |
action = e.buttonbox( "请选择您现在要做的操作:" , "智能助手" , list ) |
elif action = = list [ 4 ]: |
WeatherGet() |
action = e.buttonbox( "请选择您现在要做的操作:" , "智能助手" , list ) |
elif action = = list [ 5 ]: |
IntelligentTools() |
action = e.buttonbox( "请选择您现在要做的操作:" , "智能助手" , list ) |
exit() |
|
SignIn() |
start_Ask() |