LoginSignup
2
3

More than 5 years have passed since last update.

Pythonでファイル最終行の文字列をGoogle検索

Posted at

研究していた時に必要になったのでメモ程度に
主に以下のものを参考にしました
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

言い訳

他の記事のコードをそのまま流用し、秘伝のタレ注ぎ足しな感じで作りました
元々プログラミングから逃げてきた人が、急いで作ったコードなので色々ブサイクなコードだとは自分でも思ってる

2
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3