話不多說先上程式碼
import requests
import json
import tkinter as tk
import urllib
import re #正則
from PIL import Image,ImageTk
from tkinter import ttk
import random
win = tk.Tk()
win.geometry("700x400")
win.title("檢索器")
ccccc = tk.Label(win,text="請輸入詞條")
ccccc.place(x=10, y=20, height=15)
bbb = tk.Entry(win,width=15)
bbb.place(x=75, y=20, width=100, height=16)
"""
詞條"""
def aaa():
shuru = str(bbb.get())
url = "https://fanyi.baidu.com/sug"
"""UA偽裝"""
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.68"
}
data = {
"kw":shuru
}
"""發起請求"""
response=requests.post(url=url,data=data,headers=headers)
dic_obj=response.json()
a = dic_obj.get("data")
print("已成功獲取網址資訊")
print("資料處理中...")
for i in a:
j=i.get("k")
j1=i.get("v")
f=j,j1
ee.insert("end", f)
ee.insert("end","\n")
ee.insert("end", "\n")
print("處理完成")
button = tk.Button(win, text="詞條檢索", command=aaa)
button.place(x=195, y=17, width=100, height=25)
ee =tk.Text(win)
ee.place(x=10, y=70, width=325, height=300)
"""背景"""
global img0
photo = Image.open("u=2674208085,2485586514&fm=26&gp=0.jpg") #這個括號裡是你的背景圖片,如果不想設定背景可以直接刪除 背景 程式碼塊
photo = photo.resize((325, 300))
img0 = ImageTk.PhotoImage(photo)
img1 = ttk.Label(image=img0)
img1.place(x=350, y=70, width=325, height=300)
"""圖片
"""
def bbbbbb(j=0,Name="u=2674208085,2485586514&fm=26&gp=0.jpg"):
shuru = str(bbb.get())
# shuru1= urllib.parse.quote(shuru)
headers={"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'}
# # url = "https://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%E5%B0%8F%E7%8C%AB"
#
# url1=url + shuru1
url="https://image.baidu.com/search/index"
params={
"tn": "baiduimage",
"ipn": "r",
"ct": "201326592",
"cl": "2",
"lm": "-1",
"st": "-1",
"fm": "result",
"fr": "",
"sf": 1,
"fmq": "1602587589377_R",
"pv": "",
"ic": "undefined",
"nc": "1",
"z": "0",
"hd": "undefined",
"latest": "undefined",
"copyright": "undefined",
"se": 1,
"showtab": 0,
"fb": 0,
"width": "undefined",
"height": "undefined",
"face": 0,
"istype": 2,
"ie": "utf-8",
"ctd": "1602587589378^00_933X754",
"sid": "",
"word": shuru,
}
"""發起請求 獲得頁面"""
html = requests.get(url=url,params=params,headers=headers)
html.encoding='utf-8'
html=html.text
# Name=shuru+'.html'
# with open(Name,'w',encoding="utf-8")as fp:
# fp.write(html)
print("已成功獲取相簿網址資訊")
print("解析相簿原始碼中...")
"""通過正則 找到圖片地址"""
aaaaaa= '"objURL":"(.*?)",'
dizhi = re.findall(aaaaaa,html,re.S)
print("圖片位置資訊解析成功.")
# print(dizhi)
dizhi=dizhi[j]
# print(dizhi)
"""對圖片地址發起請求"""
pic=requests.get(dizhi,headers)
name=shuru+'.jpg'
print("圖片獲取中...")
"""儲存"""
with open(name,"wb") as f:
f.write(pic.content)
print("圖片獲取成功")
"""圖片展示到圖形化介面"""
global img0
print("圖片處理中...")
photo = Image.open(name)
photo = photo.resize((325, 300))
img0 = ImageTk.PhotoImage(photo)
img1 = ttk.Label(image=img0)
img1.place(x=350, y=70, width=325, height=300)
# img11 = ttk.Button(tab1,text="選擇照片",command = lambda : pick_photo(tab1))
print("圖片處理成功...")
button = tk.Button(win, text="圖片檢索", command=bbbbbb)
button.place(x=325, y=17, width=100, height=25)
"""同時檢索"""
def tongshi():
aaa()
bbbbbb()
button = tk.Button(win, text="同時檢索", command=tongshi)
button.place(x=455, y=17, width=100, height=25)
"""重新整理板塊"""
def aaaaaaaa():
c=random.randint(1, 10)
bbbbbb(c)
button = tk.Button(win, text="重新整理", command=aaaaaaaa)
button.place(x=600, y=17, width=50, height=25)
win.mainloop()
執行程式碼後
輸入cat後單使用詞條檢索
再點選圖片檢索
點選重新整理 更換新的圖片
輸入 pig 點選同時索引
如有問題可在評論區留言…