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)
2018-5-7 20:35 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|