关于python:python爬虫实践记录基于requests访问翻译网页爬取结果

35次阅读

共计 1778 个字符,预计需要花费 5 分钟才能阅读完成。

– codeing = utf-8 –

@Time : 2021/8/20 16:22

@File : translate.py

@Software : PyCharm

import requests
import re
import tkinter as tk
from tkinter import ttk
from tkinter import *
Findresult = re.compile(r’class=”transtext”>(.*?)</textarea>
‘)
head = {

"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67",
"Cookie":"dreye[sys_lang]=gb; phpbb3_jb4z2_u=1; phpbb3_jb4z2_k=; phpbb3_jb4z2_sid=05e7f02ea96bb4513874e06033c06885; PHPSESSID=hgsargck54cmtepq06cb39ahm0"

}
def TransLate():

url = "https://www.dreye.com.cn/trans/"
d_after.insert('0.0',"")
data = {"t_value":test.get(),
    "from": "zh-CHS",
    "to": "en",
    "sbt": "翻译",
    "t_r": ""
}
if d_com.get() == "英译中":
    data['from'] = "en"
    data['to'] = "zh-CHS"
print(data['to'])
res = ""
while len(res) == 0:
    response = requests.post(url, data=data, headers=head)
    text = response.content.decode("utf-8")
    text = str(text)
    res = re.findall(Findresult,text)[1]
d_after.insert('0.0',res)

root = tk.Tk()
root.minsize(430,200)
root.maxsize(430,200)
root.title(‘PayPal 下载千度翻译 ’)
root.attributes(“-alpha”,0.8)
l_befor = tk.Label(root,text=’ 翻译前 ’,font=10,width=10)
l_befor.grid(row=0,column=0)
d_com = ttk.Combobox(root,font=23,width=7)
d_com[‘value’] = (“ 中译英 ”,” 英译中 ”,” 待定..”)
d_com.grid(row=0,column=1)
d_com.current(0)
test = tk.Entry(root,font=100,width=50)
test.grid(row=1,columnspan=2)
l_after = tk.Label(root,text=’ 翻译后 ’,font=20,width=10)
l_after.grid(row=2,column=0)
d_after = tk.Text(root,width=50,height=3,font=100)
d_after.grid(row=3,columnspan=2)
d_button = tk.Button(root,text=’ 翻译 ’,font=25,height=2,width=11,command=TransLate)
d_button.grid(row=4,column=0)
def Call_C_B():

if v.get()==1:
    root.wm_attributes('-topmost', 1)
else:
    root.wm_attributes('-topmost', 0)
    v.set(0)

v = IntVar()
d_selet = tk.Checkbutton(root,text=” 窗口置项 ”,font=20,variable=v,command=Call_C_B)
d_selet.grid(row=4,columnspan=2)
root.mainloop()

正文完
 0