马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
大佬们,谁能帮我看看如何修改,把入门爬取有道+按键后更新,十分卡顿,不知道从哪里入手。
报错的是网页解析的,感觉是输入汉字时候需要选择所以为空,但是纯拼音输入也不可。#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 6.2
# in conjunction with Tcl version 8.6
# Nov 30, 2021 09:39:00 AM CST platform: Windows NT
from tkinter import *
from tkinter.scrolledtext import *
import urllib.request
import urllib.parse
import json
#page生成UI
top= Tk()
top.geometry("600x333+660+210")
top.minsize(152, 1)
top.maxsize(4484, 1415)
top.resizable(1, 1)
top.title("有道翻译")
top.configure(background="#d9d9d9")
Button1 = Button()
Button1.place(relx=0.417, rely=0.465, height=33, width=63)
Button1.configure(activebackground="#ececec")
Button1.configure(activeforeground="#000000")
Button1.configure(background="#d9d9d9")
Button1.configure(disabledforeground="#a3a3a3")
Button1.configure(foreground="#000000")
Button1.configure(highlightbackground="#d9d9d9")
Button1.configure(highlightcolor="black")
Button1.configure(pady="0")
Button1.configure(text="翻译",command = lambda:function())
Scrolledtext1 = ScrolledText()
Scrolledtext1.place(relx=0.05, rely=0.066, relheight=0.327
, relwidth=0.898)
Scrolledtext1.configure(background="white")
Scrolledtext1.configure(font="TkTextFont")
Scrolledtext1.configure(foreground="black")
Scrolledtext1.configure(highlightbackground="#d9d9d9")
Scrolledtext1.configure(highlightcolor="black")
Scrolledtext1.configure(insertbackground="black")
Scrolledtext1.configure(insertborderwidth="3")
Scrolledtext1.configure(selectbackground="blue")
Scrolledtext1.configure(selectforeground="white")
Scrolledtext1.configure(wrap="none")
Scrolledtext2 = ScrolledText()
Scrolledtext2.place(relx=0.05, rely=0.631, relheight=0.318
, relwidth=0.898)
Scrolledtext2.configure(background="white")
Scrolledtext2.configure(font="TkTextFont")
Scrolledtext2.configure(foreground="black")
Scrolledtext2.configure(highlightbackground="#d9d9d9")
Scrolledtext2.configure(highlightcolor="black")
Scrolledtext2.configure(insertbackground="black")
Scrolledtext2.configure(insertborderwidth="3")
Scrolledtext2.configure(selectbackground="blue")
Scrolledtext2.configure(selectforeground="white")
Scrolledtext2.configure(wrap="none")
#button点击后操作
def function():
input_word = Scrolledtext1.get(1.0,END)
print(input_word)
#将输入化为字符串'\n'连接
av = '\n'.join([i for i in input_word.splitlines() if i != ''])
print(av)
Scrolledtext2.insert("insert", fanyi(av))
#有道爬虫
def fanyi(content):
global sum
url = 'https://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
head={}
head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36'
data = {}
data['i'] =content
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] ='fanyideskweb'
data['salt'] = '16327269692950'
data['sign'] = '2381935fe41c5ed6773371fde2a3cff1'
data['lts'] = '1632726969295'
data['bv'] = '5f70acd84d315e3a3e7e05f2a4744dfa'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CLICKBUTTION'
data = urllib.parse.urlencode(data).encode('utf8')
req = urllib.request.Request(url,data,head)
response = urllib.request.urlopen(req)
html = response.read().decode('utf8')
targets=json.loads(html)
results=targets['translateResult']
#爬取值为字典需要选择输出,每个results是一组对应词典,i可以去一层括号,再使用src可获取输入的字,tgt获取翻译后的字
sum = ''
for i in results:
sum = sum+i[0]['tgt']+'\n'
return sum
top.mainloop()
|