Python通过tkinter实现百度搜索的示例代码
作者:wx_zhu6201976 时间:2022-06-28 01:28:46
本文主要介绍了Python通过tkinter实现百度搜索的示例代码,分享给大家,具体如下:
"""
百度搜索可视化
"""
import tkinter
import win32api
from selenium.webdriver import Chrome
entry = None
def callback():
global entry
keywords = entry.get()
if not keywords:
win32api.MessageBox(0, '请输入搜索关键字', '提示', 0)
return
chrome = Chrome()
chrome.get('https://www.baidu.com/')
chrome.find_element_by_id('kw').send_keys(keywords)
chrome.find_element_by_id('su').click()
# bilibili关键字搜索
# chrome.get('https://www.bilibili.com/')
# chrome.find_element_by_xpath('//form[@id="nav_searchform"]/input').send_keys(keywords)
# chrome.find_element_by_xpath('//div[@class="nav-search-btn"]/button').click()
def main():
global entry
tk = tkinter.Tk()
# tk.resizable(width=False,height=False) # 固定窗体大小?无效
tk.title('百度搜索')
# 1.设置窗体居中
# screenwidth = tk.winfo_screenwidth() # 获取屏幕宽度
# screenheight = tk.winfo_screenheight() # 获取屏幕高度
# # 计算窗体大小,位置参数,width,height:窗体宽高
# width = 100
# height = 50
# size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
# tk.geometry(size) # 设置窗体位置为屏幕居中
# 2.设置窗体右下角,无效
# screenwidth = tk.winfo_screenwidth() # 获取屏幕宽度
# screenheight = tk.winfo_screenheight() # 获取屏幕高度
# print(screenwidth,screenheight)
# # 计算窗体大小,位置参数,width,height:窗体宽高
# width = 100
# height = 50
# size = '%dx%d+%d+%d' % (width, height, (screenwidth - width), (screenheight - height))
# tk.geometry(size) # 设置窗体位置为屏幕右下角
# 获取窗体x,y
# tk.update()
# print(tk.winfo_x())
# print(tk.winfo_y())
tk.geometry('+0+0') # 固定屏幕左上角
# tk.geometry('+1440+770')
entry = tkinter.Entry(tk)
entry.pack()
button = tkinter.Button(tk, text='百度一下', command=callback)
button.pack()
tk.mainloop()
if __name__ == '__main__':
main()
补充:python模拟百度搜索点击链接
# coding: utf-8
import os
import time
import requests
import urllib.parse
from bs4 import BeautifulSoup
from urllib.parse import urlparse
from fake_useragent import UserAgent
from multiprocessing.pool import ThreadPool
LOCATIONS = {}
GLOBAL_THREAD = 500
GLOBAL_TIMEOUT = 50
def get_links(keyword, generator, pages):
links = []
for page in range(int(pages.split("-")[0]), int(pages.split("-")[1]) + 1):
for genera in range(int(generator.split("-")[0]), int(generator.split("-")[1]) + 1):
links.append(
"http://www.baidu.com.cn/s?wd=" + urllib.parse.quote(keyword + str(genera)) + "&pn=" + str(page * 10))
return links
def get_page(url):
headers = {"user-agent": UserAgent().chrome}
req = requests.get(url, headers=headers)
req.encoding = "utf-8"
soup = BeautifulSoup(req.text, "lxml")
for link in soup.select("div.result > h3.t > a"):
req = requests.get(link.get("href"), headers=headers, allow_redirects=False)
if "=" in req.headers["location"]:
root = urlparse(req.headers["location"]).netloc
LOCATIONS[root] = req.headers["location"]
def baidu_search():
try:
os.system("cls")
print("-" * 56 + "\n")
print("| BaiduSearch Engine By 美图博客[https://www.meitubk.com/] |\n")
print("-" * 56 + "\n")
keyword = input("Keyword: ")
generator = input("Generator(1-10): ")
pages = input("Pages(0-10): ")
start = time.time()
pool = ThreadPool(processes=GLOBAL_THREAD)
pool.map(get_page, get_links(keyword, generator, pages))
pool.close()
pool.join()
end = time.time()
path = r"D:\Desktop\result.txt"
save_result(path)
print("\nSava in %s" % path)
print("Result count: %d" % len(LOCATIONS.values()))
print("Running time: %ds" % (end - start))
except:
print("\nInput Error!")
exit(0)
def save_result(path):
with open(path, "w") as file:
for url in list(LOCATIONS.values()):
file.write(url + "\n")
baidu_search()
来源:https://blog.csdn.net/zhu6201976/article/details/103941292
标签:Python,tkinter,百度搜索
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Go语言数据结构之插入排序示例详解
2024-05-05 09:34:32
![](https://img.aspxhome.com/file/2023/7/127457_0s.jpg)
python如何利用matplotlib绘制并列双柱状图并标注数值
2022-08-06 22:00:33
![](https://img.aspxhome.com/file/2023/8/123798_0s.png)
纠结于ajax开发中 response的contentType 问题
2008-12-11 13:46:00
git设置忽略文件.gitignore的方法
2023-05-18 02:12:19
Java开发之Spring连接数据库方法实例分析
2024-01-26 02:00:54
Python机器学习工具scikit-learn的使用笔记
2021-08-30 10:28:50
![](https://img.aspxhome.com/file/2023/4/100914_0s.png)
vue3动态加载对话框的方法实例
2024-05-09 15:16:55
![](https://img.aspxhome.com/file/2023/9/126509_0s.png)
详解Python 中的容器 collections
2021-09-12 07:11:38
python实现一组典型数据格式转换
2023-07-14 22:19:04
![](https://img.aspxhome.com/file/2023/9/115599_0s.jpg)
淘宝网提供的国内NPM镜像简介和使用方法
2024-04-17 10:10:16
python实现简单颜色识别程序
2022-07-15 09:34:47
![](https://img.aspxhome.com/file/2023/4/121094_0s.jpg)
python pyqtgraph 保存图片到本地的实例
2023-05-08 01:15:11
![](https://img.aspxhome.com/file/2023/4/77764_0s.jpg)
Python中getattr函数和hasattr函数作用详解
2022-10-29 15:42:11
pytest测试框架+allure超详细教程
2023-03-18 21:38:00
![](https://img.aspxhome.com/file/2023/6/69506_0s.png)
解决编码问题:UnicodeDecodeError: 'utf-8' codec can't decod
2023-03-25 02:45:59
![](https://img.aspxhome.com/file/2023/8/103148_0s.png)
基于Python实现五子棋游戏
2023-01-29 21:47:42
![](https://img.aspxhome.com/file/2023/1/65911_0s.jpg)
Python Matplotlib绘制箱线图的全过程
2023-12-03 21:37:39
![](https://img.aspxhome.com/file/2023/7/127737_0s.png)
详解vue配置请求多个服务端解决方案
2024-05-05 09:06:11
python 装饰器的基本使用
2021-04-01 07:12:50
python实现删除文件与目录的方法
2023-11-12 23:34:46