標題:
python爬蟲源碼(爬天氣及詞典)
[打印本頁]
作者:
林調(diào)云
時間:
2018-5-7 20:36
標題:
python爬蟲源碼(爬天氣及詞典)
python爬蟲爬天氣及詞典
單片機源程序如下:
from tkinter import *
import urllib.request
import urllib.parse
import json
from city import city
#天氣預(yù)報函數(shù)
def weather():
#構(gòu)造Application對象
class Application(Frame):
#構(gòu)建基本窗框模板
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets() #調(diào)用函數(shù)
def create_widgets(self):
#添加標題名稱,并以表格的方式規(guī)定其位置
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第幾列
#寫入內(nèi)容,并以表格的方式規(guī)定其位置
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訪問函數(shù),并以表格的方式規(guī)定其位置
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)寫入的數(shù)據(jù)
cityname = self.text1.get()
citycode = city.get(cityname)
#構(gòu)造網(wǎng)址
url = ('http://www.weather.com.cn/data/cityinfo/%s.html'%citycode)
content = urllib.request.urlopen(url).read()#讀取網(wǎng)頁源代碼
data = json.loads(content.decode())#使用json庫將字符轉(zhuǎn)化為字典
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')#設(shè)置head
result = data['weatherinfo']#獲取字典
self.text2.delete(0,END) #擦除text2數(shù)據(jù)
#將爬蟲所得數(shù)據(jù)寫入
self.text2.insert(0,result['weather']) #將爬蟲所得數(shù)據(jù)寫入text2
self.text3.delete(0,END)
self.text3.insert(0,result['temp1'])
self.text4.delete(0,END)
self.text4.insert(0,result['temp2'])
#清除數(shù)據(jù)
def clear(self):
self.text1.delete(0,END) #擦除text2數(shù)據(jù)
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() #創(chuàng)建窗口對象給root
root.title('天氣預(yù)報') #寫窗口標題
root.geometry('400x200') #規(guī)定窗口大小
app = Application(root) #調(diào)用Application對象
app.mainloop() #循環(huán)窗口
#有道翻譯函數(shù)
def interpretation():
#構(gòu)造Application對象
class Application(Frame):
#構(gòu)建基本窗框模板
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
#添加標題名稱,并以表格的方式規(guī)定其位置
self.label1 = Label(self, text='請輸入需要翻譯的內(nèi)容:')
self.label1.grid(row=0,column=0)
self.label2 = Label(self, text=' ')
self.label2.grid(row=2,column=0)
#寫入內(nèi)容,并以表格的方式規(guī)定其位置
self.text1 = Entry(self)
self.text1.grid(row=0,column=1)
self.text2 = Entry(self)
self.text2.grid(row=2,column=1)
#按鍵,通過command訪問函數(shù),并以表格的方式規(guī)定其位置
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)寫入的數(shù)據(jù)
content = self.text1.get()
#構(gòu)造網(wǎng)址
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
#讀取網(wǎng)頁數(shù)據(jù)
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')
#給網(wǎng)頁寫入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庫將字符轉(zhuǎn)化為字典
target = json.loads(html)
self.text2.delete(0,END)
self.text2.insert(0,target['translateResult'][0][0]['tgt'])
#清除數(shù)據(jù)
def clear(self):
self.text1.delete(0,END)
self.text1.focus_set()
self.text2.delete(0,END)
self.text2.focus_set()
root = Tk()#創(chuàng)建窗口對象給root
root.title('翻譯') #寫窗口標題
root.geometry('400x200')#規(guī)定窗口大小
app = Application(root)#調(diào)用Application對象
app.mainloop() #循環(huán)窗口
#畫圖函數(shù)
def drawing():
root = Tk()#創(chuàng)建窗口對象給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函數(shù)綁定鼠標左鍵
Label(root, text="按住鼠標左鍵并移動,開始繪制你的理想藍圖吧......").pack(side=BOTTOM)#最下行提示語句
mainloop() #循環(huán)窗口
root = Tk()#創(chuàng)建窗口對象給root
root.title("菜單")#寫窗口標題
menubar = Menu(root)#創(chuàng)建Menu類的實例
root.geometry("400x400+600+200") #大小400*400,在桌面位置600列,200行
menubar1=menubar.add_command(label="天氣預(yù)報", command=weather)#add方法添加命令或者其他菜單內(nèi)容 command按鍵調(diào)用weather函數(shù)
menubar2=menubar.add_command(label="有道翻譯", command=interpretation)#add方法添加命令或者其他菜單內(nèi)容 command按鍵調(diào)用interpretation函數(shù)
menubar3=menubar.add_command(label="畫圖", command=drawing)#add方法添加命令或者其他菜單內(nèi)容 command按鍵調(diào)用drawing函數(shù)
root.config(menu=menubar)#顯示菜單
復制代碼
所有資料51hei提供下載:
爬蟲.rar
(18.54 KB, 下載次數(shù): 42)
2018-5-7 20:35 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
作者:
cadasavdsv
時間:
2020-12-21 18:28
這個要怎么用?
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1