📅  最后修改于: 2023-12-03 15:42:30.116000             🧑  作者: Mango
颤振命名导航是一款基于Python语言开发的命令行程序,通过输入关键词,能够实现快速搜索相关内容,并展示给用户,方便用户快速查找所需信息。
安装Python环境。
克隆或下载颤振命名导航的代码库。
进入代码库所在目录,执行以下命令安装所需依赖:
pip install -r requirements.txt
在命令行中,执行以下命令启动颤振命名导航:
python main.py
根据提示输入关键词进行搜索,并选择相应的选项进行查看。
# 安装相关依赖
pip install requests beautifulsoup4
# 搜索引擎类
class SearchEngine:
def __init__(self, name, url_pattern=None, headers=None):
self.name = name
self.url_pattern = url_pattern
self.headers = headers
def search(self, keyword):
if self.url_pattern and self.headers:
url = self.url_pattern.format(keyword=quote(keyword))
response = requests.get(url, headers=self.headers)
soup = BeautifulSoup(response.text, 'html.parser')
items = self.parse_soup(soup)
return items
return []
def parse_soup(self, soup):
pass
# 百度搜索引擎类
class BaiduSearchEngine(SearchEngine):
def __init__(self):
super().__init__('百度', 'https://www.baidu.com/s?wd={keyword}', {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
})
def parse_soup(self, soup):
elements = soup.select('.result')
items = []
for elem in elements:
title_elem = elem.select_one('.t a')
href = title_elem['href']
title = title_elem.text.strip()
abstract_elem = elem.select_one('.c-abstract')
abstract = abstract_elem.text.strip() if abstract_elem else ''
items.append({'title': title, 'href': href, 'abstract': abstract})
return items