加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
google.py 881 Bytes
一键复制 编辑 原始数据 按行查看 历史
Jérôme Krell 提交于 2019-10-10 14:22 . Reformat Code by PyCharm-Community
"""
Author: Ankit Agarwal (ankit167)
Usage: python google.py <keyword>
Description: Script googles the keyword and opens
top 5 (max) search results in separate
tabs in the browser
Version: 1.0
"""
import sys
import webbrowser
import bs4
import pyperclip
import requests
def main():
if len(sys.argv) > 1:
keyword = ' '.join(sys.argv[1:])
else:
# if no keyword is entered, the script would search for the keyword
# copied in the clipboard
keyword = pyperclip.paste()
res = requests.get('http://google.com/search?q=' + keyword)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text)
linkElems = soup.select('.r a')
numOpen = min(5, len(linkElems))
for i in range(numOpen):
webbrowser.open('http://google.com' + linkElems[i].get('href'))
if __name__ == '__main__':
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化