Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google検索して、結果の上位5個を、ブラウザーを起動して表示するPythonスクリプト
# -*- coding:utf-8 -*-
"""
Google検索ツール
https://automatetheboringstuff.com/chapter11/
"""
import requests
import sys
import webbrowser
import bs4
def main():
# search and get result page.
res = requests.get('http://google.com/search?q=' + ' '.join(sys.argv[1:]))
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
anchors = soup.select('.r a')
num_open = min(5, len(anchors))
for i in range(num_open):
webbrowser.open('http://google.co.jp' + anchors[i].get('href'))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.