python爬虫爬天气及词典
单片机源程序如下:
- from tkinter import *
- import urllib.request
- import urllib.parse
- import json
- from city import city
- #天气预报函数
- def weather():
- #构造Application对象
- class Application(Frame):
- #构建基本窗框模板
- def __init__(self, master):
- super(Application, self).__init__(master)
- self.grid()
- self.create_widgets() #调用函数
- def create_widgets(self):
- #添加标题名称,并以表格的方式规定其位置
- self.label1 = Label(self, text='城市')
- self.label1.grid(row=0,column=0)
- self.label2 = Label(self, text=' ')
- self.label2.grid(row=2,column=0)
- self.label3 = Label(self, text='最低气温')
- self.label3.grid(row=4,column=0)
- self.label4 = Label(self, text='最高气温')
- self.label4.grid(row=6,column=0) #row 第几行;column第几列
-
- #写入内容,并以表格的方式规定其位置
- self.text1 = Entry(self)
- self.text1.grid(row=0,column=1)
- self.text2 = Entry(self)
- self.text2.grid(row=2,column=1)
- self.text3 = Entry(self)
- self.text3.grid(row=4,column=1)
- self.text4 = Entry(self)
- self.text4.grid(row=6,column=1)
- #按键,通过command访问函数,并以表格的方式规定其位置
- self.botton1 = Button(self,text='确认', command=self.weather)
- self.botton1.grid(row=0, column=2)
- self.botton2 = Button(self,text='重新输入', command=self.clear)
- self.botton2.grid(row=8, column=3)
-
- #确定光标位置
- self.text1.focus_set()
- self.text2.focus_set()
- self.text3.focus_set()
- self.text4.focus_set()
- #爬虫部分
- def weather(self):
- #get得到self.text1 = Entry(self)写入的数据
- cityname = self.text1.get()
- citycode = city.get(cityname)
- #构造网址
- url = ('http://www.weather.com.cn/data/cityinfo/%s.html'%citycode)
- content = urllib.request.urlopen(url).read()#读取网页源代码
- data = json.loads(content.decode())#使用json库将字符转化为字典
- req = urllib.request.Request(url, data)
- req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36')#设置head
- result = data['weatherinfo']#获取字典
- self.text2.delete(0,END) #擦除text2数据
- #将爬虫所得数据写入
- self.text2.insert(0,result['weather']) #将爬虫所得数据写入text2
- self.text3.delete(0,END)
- self.text3.insert(0,result['temp1'])
- self.text4.delete(0,END)
- self.text4.insert(0,result['temp2'])
-
-
- #清除数据
- def clear(self):
- self.text1.delete(0,END) #擦除text2数据
- self.text1.focus_set() #确定光标位置
- self.text2.delete(0,END)
- self.text2.focus_set()
- self.text3.delete(0,END)
- self.text3.focus_set()
- self.text4.delete(0,END)
- self.text4.focus_set()
- root = Tk() #创建窗口对象给root
- root.title('天气预报') #写窗口标题
- root.geometry('400x200') #规定窗口大小
- app = Application(root) #调用Application对象
- app.mainloop() #循环窗口
- #有道翻译函数
- def interpretation():
- #构造Application对象
- class Application(Frame):
- #构建基本窗框模板
- def __init__(self, master):
- super(Application, self).__init__(master)
- self.grid()
- self.create_widgets()
-
- def create_widgets(self):
- #添加标题名称,并以表格的方式规定其位置
- self.label1 = Label(self, text='请输入需要翻译的内容:')
- self.label1.grid(row=0,column=0)
- self.label2 = Label(self, text=' ')
- self.label2.grid(row=2,column=0)
- #写入内容,并以表格的方式规定其位置
- self.text1 = Entry(self)
- self.text1.grid(row=0,column=1)
- self.text2 = Entry(self)
- self.text2.grid(row=2,column=1)
- #按键,通过command访问函数,并以表格的方式规定其位置
- self.botton1 = Button(self,text='确认', command=self.translation)
- self.botton1.grid(row=0, column=2)
- self.botton2 = Button(self,text='重新输入', command=self.clear)
- self.botton2.grid(row=4, column=1)
- self.text1.focus_set()
- self.text2.focus_set()
- #爬虫部分
- def translation(self):
- #get得到self.text1 = Entry(self)写入的数据
- content = self.text1.get()
- #构造网址
- url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
- #读取网页数据
- data = {}
- data['type'] = 'AUTO'
- data['i'] = content
- data['doctype'] = 'json'
- data['version'] = '2.1'
- data['keyfrom'] = 'fanyi.web'
- data['ue'] = 'UTF-8'
- data['typoResult'] = 'true'
- data = urllib.parse.urlencode(data).encode('utf-8')
-
- #给网页写入head(防止反爬虫)
- req = urllib.request.Request(url, data)
- req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36')
-
- response = urllib.request.urlopen(req)
- html = response.read().decode('utf-8')
- #使用json库将字符转化为字典
- target = json.loads(html)
-
- self.text2.delete(0,END)
- self.text2.insert(0,target['translateResult'][0][0]['tgt'])
-
- #清除数据
- def clear(self):
- self.text1.delete(0,END)
- self.text1.focus_set()
- self.text2.delete(0,END)
- self.text2.focus_set()
- root = Tk()#创建窗口对象给root
- root.title('翻译') #写窗口标题
- root.geometry('400x200')#规定窗口大小
- app = Application(root)#调用Application对象
- app.mainloop() #循环窗口
- #画图函数
- def drawing():
- root = Tk()#创建窗口对象给root
- w = Canvas(root, bg='red', width=400, height=200) # 在界面中画出一个400*200的画布,画布颜色红色
- w.pack()
- def paint(event):
- x1, y1 = (event.x - 1), (event.y - 1)
- x2, y2 = (event.x + 1), (event.y + 1)
- w.create_oval(x1, y1, x2, y2, fill="blue")#画一个小椭圆,蓝色填充
- w.bind("<B1-Motion>", paint)#paint函数绑定鼠标左键
- Label(root, text="按住鼠标左键并移动,开始绘制你的理想蓝图吧......").pack(side=BOTTOM)#最下行提示语句
- mainloop() #循环窗口
- root = Tk()#创建窗口对象给root
- root.title("菜单")#写窗口标题
- menubar = Menu(root)#创建Menu类的实例
- root.geometry("400x400+600+200") #大小400*400,在桌面位置600列,200行
- menubar1=menubar.add_command(label="天气预报", command=weather)#add方法添加命令或者其他菜单内容 command按键调用weather函数
- menubar2=menubar.add_command(label="有道翻译", command=interpretation)#add方法添加命令或者其他菜单内容 command按键调用interpretation函数
- menubar3=menubar.add_command(label="画图", command=drawing)#add方法添加命令或者其他菜单内容 command按键调用drawing函数
- root.config(menu=menubar)#显示菜单
复制代码
所有资料51hei提供下载:
爬虫.rar
(18.54 KB, 下载次数: 42)
|