服务器之家

服务器之家 > 正文

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

时间:2021-10-07 10:43     来源/作者:wx_zhu6201976

本文主要介绍了Python通过tkinter实现百度搜索的示例代码,分享给大家,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
百度搜索可视化
"""
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模拟百度搜索点击链接

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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百度搜索内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/zhu6201976/article/details/103941292

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部