本帖最后由 wushaominkk 于 2018-5-9 09:41 编辑
刚学python不久,自己摸索着做了一个通过爬取百度翻译的翻译器,希望能对大家有些帮助,代码如下
[Python] 纯文本查看 复制代码 #!/usr/bin/python
# -*- coding: cp936 -*-
###################################################
#基于百度翻译的爬虫翻译器
#by pengyao1207
#2017/10/28
###################################################
from ScrolledText import ScrolledText
from Tkinter import *
import requests
# ______________清屏_______________#
def qp():
e.delete("1.0", 'end')
d.delete("1.0", 'end')
# ______________翻译______________#
def fy():
e.delete("1.0", 'end')
url = 'http://fanyi.baidu.com/v2transapi/'
datelist = d.get('1.0', 'end').split('\n')
for one in range(0, len(datelist) - 1):
if datelist[one] == '':
e.insert(AtEnd(), '\n')
continue
data = {'query': datelist[one]}
response = requests.post(url, data)
e.insert(AtEnd(), response.json()['trans_result']['data'][0]['dst'] + '\n')
# ________main________#
root = Tk()
root.title("翻译器 made by pengyao1207".decode("gbk"))
root.geometry('800x600')
root.resizable(width=False, height=False)
d = ScrolledText(root, width=100, height=20)
d.pack(side=TOP)
frm1 = Frame(root)
fy = Button(frm1, text="翻译".decode('gbk'), font=("Arial", 12), width=5, height=2, command=fy).pack(side=LEFT)
Label(frm1, text=" ", width=20, height=2).pack(side=LEFT)
Button(frm1, text="清屏".decode('gbk'), font=("Arial", 12), width=5, height=2, command=qp).pack(side=LEFT)
frm1.pack(side=TOP)
dfy = StringVar()
e = ScrolledText(root, width=100, height=20)
e.pack(side=TOP)
root.mainloop()
附件中也有代码~
软件虽然简单,可是用来python爬虫入门确非常好的~~
补充一下:我用的是python2.7 |