Python通過(guò)tkinter實(shí)現(xiàn)百度搜索的示例代碼
本文主要介紹了Python通過(guò)tkinter實(shí)現(xiàn)百度搜索的示例代碼,分享給大家,具體如下:
""" 百度搜索可視化 """ import tkinter import win32api from selenium.webdriver import Chrome entry = None def callback(): global entry keywords = entry.get() if not keywords: win32api.MessageBox(0, '請(qǐng)輸入搜索關(guān)鍵字', '提示', 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關(guān)鍵字搜索 # 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) # 固定窗體大小?無(wú)效 tk.title('百度搜索') # 1.設(shè)置窗體居中 # screenwidth = tk.winfo_screenwidth() # 獲取屏幕寬度 # screenheight = tk.winfo_screenheight() # 獲取屏幕高度 # # 計(jì)算窗體大小,位置參數(shù),width,height:窗體寬高 # width = 100 # height = 50 # size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) # tk.geometry(size) # 設(shè)置窗體位置為屏幕居中 # 2.設(shè)置窗體右下角,無(wú)效 # screenwidth = tk.winfo_screenwidth() # 獲取屏幕寬度 # screenheight = tk.winfo_screenheight() # 獲取屏幕高度 # print(screenwidth,screenheight) # # 計(jì)算窗體大小,位置參數(shù),width,height:窗體寬高 # width = 100 # height = 50 # size = '%dx%d+%d+%d' % (width, height, (screenwidth - width), (screenheight - height)) # tk.geometry(size) # 設(shè)置窗體位置為屏幕右下角 # 獲取窗體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()
補(bǔ)充:python模擬百度搜索點(diǎn)擊鏈接
# 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()
到此這篇關(guān)于Python通過(guò)tkinter實(shí)現(xiàn)百度搜索的示例代碼的文章就介紹到這了,更多相關(guān)Python tkinter百度搜索內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python使用Selenium自動(dòng)進(jìn)行百度搜索的實(shí)現(xiàn)
- Python爬蟲(chóng)爬取百度搜索內(nèi)容代碼實(shí)例
- python采集百度搜索結(jié)果帶有特定URL的鏈接代碼實(shí)例
- python+selenium實(shí)現(xiàn)自動(dòng)化百度搜索關(guān)鍵詞
- python實(shí)現(xiàn)百萬(wàn)答題自動(dòng)百度搜索答案
- python實(shí)現(xiàn)提取百度搜索結(jié)果的方法
- Python實(shí)現(xiàn)抓取百度搜索結(jié)果頁(yè)的網(wǎng)站標(biāo)題信息
- Python10行代碼實(shí)現(xiàn)模擬百度搜索的示例
相關(guān)文章
Python爬取微信讀書(shū)實(shí)現(xiàn)讀書(shū)免費(fèi)自由
主要跟大家介紹一下,我是如何用Python爬取小說(shuō),再導(dǎo)入微信讀書(shū)的。成功實(shí)現(xiàn)在微信讀書(shū)中各種“白票”付費(fèi)小說(shuō),有需要的朋友可以借鑒參考下2021-09-09Python數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹(shù)結(jié)構(gòu)定義與遍歷方法詳解
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹(shù)結(jié)構(gòu)定義與遍歷方法,結(jié)合實(shí)例形式詳細(xì)分析了Python實(shí)現(xiàn)二叉樹(shù)結(jié)構(gòu)的定義、遍歷方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-12-12Python圖像處理庫(kù)PIL詳細(xì)使用說(shuō)明
Pillow是Python中較為基礎(chǔ)的圖像處理庫(kù),主要用于圖像的基本處理,比如裁剪圖像、調(diào)整圖像大小和圖像顏色處理等,需要的朋友可以參考下2022-04-04Python開(kāi)發(fā)畢設(shè)案例之桌面學(xué)生信息管理程序
畢業(yè)設(shè)計(jì)必備案例:Python開(kāi)發(fā)桌面程序2021-11-11Python+Pygame實(shí)現(xiàn)彩色五子棋游戲
這篇文章主要為大家詳細(xì)介紹了如何溧陽(yáng)Python和Pygame實(shí)現(xiàn)彩色五子棋游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02使用Python Tkinter創(chuàng)建文件生成工具的操作步驟
我們將使用Python的Tkinter模塊創(chuàng)建一個(gè)簡(jiǎn)單的文件生成工具,這個(gè)工具可以用來(lái)創(chuàng)建Excel、文本、Python腳本和Word文檔等不同類(lèi)型的文件,感興趣的朋友可以參考下2024-04-04python高級(jí)內(nèi)置函數(shù)用法實(shí)例
在本篇文章里小編給大家整理的是一篇關(guān)于python高級(jí)內(nèi)置函數(shù)用法實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)參考下。2021-09-09python生成詞云的實(shí)現(xiàn)方法(推薦)
下面小編就為大家?guī)?lái)一篇python生成詞云的實(shí)現(xiàn)方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06