Python通过tkinter实现百度搜索的示例代码

 更新时间:2021年04月09日 09:29:27   作者:wx_zhu6201976  
这篇文章主要介绍了Python通过tkinter实现百度搜索的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

本文主要介绍了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()

到此这篇关于Python通过tkinter实现百度搜索的示例代码的文章就介绍到这了,更多相关Python tkinter百度搜索内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 简单了解django索引的相关知识

    简单了解django索引的相关知识

    这篇文章主要介绍了简单了解django索引的相关知识,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 深入浅析Python数据分析的过程记录

    深入浅析Python数据分析的过程记录

    我们先利用 Jupyter Notebook 来进行分析,然后,在得到成果以后,利用 Pycharm 来进行完整的程序设计,对Python数据分析的过程记录感兴趣的朋友一起看看吧
    2022-01-01
  • Python Base64编码和解码操作

    Python Base64编码和解码操作

    Base64 就是一种基于64个可打印字符来表示二进制数据的方法,这篇文章主要介绍了Python Base64编码和解码,需要的朋友可以参考下
    2022-12-12
  • pandas dataframe按照列名给列排序三种方法

    pandas dataframe按照列名给列排序三种方法

    这篇文章主要给大家介绍了关于pandas dataframe按照列名给列排序的三种方法,在进行数据分析操作时,经常需要对数据按照某行某列排序,或者按照多行多列排序,以及按照索引值排序等等,需要的朋友可以参考下
    2023-07-07
  • python随机在一张图像上截取任意大小图片的方法

    python随机在一张图像上截取任意大小图片的方法

    今天小编就为大家分享一篇python随机在一张图像上截取任意大小图片的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • Python基于分析Ajax请求实现抓取今日头条街拍图集功能示例

    Python基于分析Ajax请求实现抓取今日头条街拍图集功能示例

    这篇文章主要介绍了Python基于分析Ajax请求实现抓取今日头条街拍图集功能,涉及Python针对今日头条URL请求与json数据处理相关操作技巧,需要的朋友可以参考下
    2018-07-07
  • python实现聚类算法原理

    python实现聚类算法原理

    这篇文章主要为大家详细介绍了python实现聚类算法原理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • Django保护敏感信息的方法示例

    Django保护敏感信息的方法示例

    这篇文章主要介绍了Django保护敏感信息的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05
  • Python web如何在IIS发布应用过程解析

    Python web如何在IIS发布应用过程解析

    这篇文章主要介绍了Python web如何在IIS发布应用过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • python hashlib加密实现代码

    python hashlib加密实现代码

    这篇文章主要介绍了python hashlib加密实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10

最新评论