Created
October 6, 2017 06:28
-
-
Save dev001hajipro/2db62c5e6e70598d53b18f6247777f60 to your computer and use it in GitHub Desktop.
Google検索して、結果の上位5個を、ブラウザーを起動して表示するPythonスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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