研究していた時に必要になったのでメモ程度に
主に以下のものを参考にしました
http://qiita.com/code_monkey/items/0d38a92a2a2026f5ec82
http://qiita.com/Kodaira_/items/eb5cdef4c4e299794be3
準備
- Python2.7
- Google パッケージ
あらかじめGoogleパッケージをpip install google
などで入れといてください
コード
search.py
import linecache
from google import search
def google_search(query, limit=1):
for url in search(query, lang="jp", num=limit, stop=1):
print url
def main():
num_lines = sum(1 for line in open('hoge.txt'))
target_line = linecache.getline('hoge.txt', num_lines)
print(target_line)
linecache.clearcache()
google_search(target_line)
if __name__ == '__main__':
main()
ファイルにはこんな感じ
hoge.txt
hello python
ok google
hey siri
実行結果
hey siri
https://dekiru.net/article/5312/
Appleの公式のページが一番上に来ないんだね...orz
言い訳
他の記事のコードをそのまま流用し、秘伝のタレ注ぎ足しな感じで作りました
元々プログラミングから逃げてきた人が、急いで作ったコードなので色々ブサイクなコードだとは自分でも思ってる