Skip to content

Instantly share code, notes, and snippets.

@dev001hajipro
Created October 6, 2017 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dev001hajipro/2db62c5e6e70598d53b18f6247777f60 to your computer and use it in GitHub Desktop.
Save dev001hajipro/2db62c5e6e70598d53b18f6247777f60 to your computer and use it in GitHub Desktop.
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