按照教程自己写了个可以简单进行网站查询的工具,在pycharm里能够正常运行,在资源管理器里双击文件就会闪退,希望大佬看看代码有没有什么问题
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class Search:
def __init__(self, text):
self.text = text
self.driver = None
def open_web(self):
global driver
driver = webdriver.Edge(r'C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe')
driver.maximize_window()
self.driver = driver
def text_solve(self):
if self.text[0:2].lower() == 'bd' \
or self.text[0:2].lower() == 'bg' \
or self.text[0:2].lower() == 'cd':
self.text = list(self.text)
del self.text[0:2]
self.text = ''.join(self.text)
def search_web(self):
bd_url = 'https://www.baidu.com/'
bing_url = 'https://cn.bing.com/?FORM=Z9FD1'
csdn_url = 'https://www.csdn.net/?spm=1000.2115.3001.5359'
if self.text[0:2].lower() == 'bd':
self.driver.get(bd_url)
self.text_solve()
self.driver.find_element_by_xpath('//*[@id="kw"]').send_keys(self.text)
self.driver.find_element_by_xpath('//*[@id="kw"]').send_keys(Keys.ENTER)
elif self.text[0:2].lower() == 'bg':
self.driver.get(bing_url)
self.text_solve()
self.driver.find_element_by_xpath('//*[@id="sb_form_q"]').send_keys(self.text)
self.driver.find_element_by_xpath('//*[@id="sb_form_q"]').send_keys(Keys.ENTER)
elif self.text[0:2].lower() == 'cd':
self.driver.get(csdn_url)
self.text_solve()
self.driver.find_element_by_xpath('//*[@id="toolbar-search-input"]').send_keys(self.text)
self.driver.find_element_by_xpath('//*[@id="toolbar-search-input"]').send_keys(Keys.ENTER)
def run(self):
self.open_web()
self.search_web()
if __name__ == '__main__':
while True:
text = input('请输入搜索内容:')
if text[0:2].lower() == 'bd' \
or text[0:2].lower() == 'bg' \
or text[0:2].lower() == 'cd':
Search(text).run()
break
else:
print('识别错误~')
print('请在搜索内容前加关键词(bd:百度;bg:必应;cd:CSDN)')
|