国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python通過tkinter實現百度搜索的示例代碼

瀏覽:89日期:2022-06-22 18:31:50

本文主要介紹了Python通過tkinter實現百度搜索的示例代碼,分享給大家,具體如下:

'''百度搜索可視化'''import tkinter import win32apifrom 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-8import osimport timeimport requestsimport urllib.parsefrom bs4 import BeautifulSoupfrom urllib.parse import urlparsefrom fake_useragent import UserAgentfrom multiprocessing.pool import ThreadPoolLOCATIONS = {}GLOBAL_THREAD = 500GLOBAL_TIMEOUT = 50def 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 linksdef 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']).netlocLOCATIONS[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:Desktopresult.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百度搜索內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: 百度 Python
相關文章:
主站蜘蛛池模板: 巨鹿县| 达拉特旗| 潞西市| 元阳县| 长宁区| 和田县| 军事| 巴东县| 镇沅| 昌邑市| 红河县| 绿春县| 栖霞市| 瑞昌市| 绥棱县| 丹江口市| 出国| 那坡县| 八宿县| 南召县| 崇左市| 辽源市| 德令哈市| 钟山县| 兴安县| 县级市| 布拖县| 新丰县| 兴宁市| 贺兰县| 南充市| 东丽区| 柘城县| 河东区| 建湖县| 景谷| 平乐县| 中阳县| 峡江县| 镇康县| 顺义区|