Google search for the last line of the file in Python

I needed it when I was researching, so it's just a memo I mainly referred to the following http://qiita.com/code_monkey/items/0d38a92a2a2026f5ec82 http://qiita.com/Kodaira_/items/eb5cdef4c4e299794be3

Preparation

Please insert the Google package in advance with pip install google etc.

code

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()

The file looks like this

hoge.txt


hello python
ok google
hey siri

Execution result

hey siri

https://dekiru.net/article/5312/

Apple's official page doesn't come to the top ... orz

excuse

I used the code from other articles as it is and made it with a secret sauce. I think that it is a lot of ugly code because it is a code that a person who originally escaped from programming made in a hurry

Recommended Posts

Google search for the last line of the file in Python
Various ways to read the last line of a csv file in Python
Read the file line by line in Python
Read the file line by line in Python
In search of the fastest FizzBuzz in Python
[Python] Read the specified line in the file
[Python] Explore the characteristics of the titles of the top sites in Google search results
Download the file in Python
Search for strings in Python
Check the operation of Python for .NET in each environment
Save the binary file in Python
[Note] Import of a file in the parent directory in Python
The story of the "hole" in the file
Output the specified table of Oracle database in Python to Excel for each file
[Python] The biggest weakness / disadvantage of Google Colaboratory [For beginners]
Wrap (part of) the AtCoder Library in Cython for use in Python
[Python] LINE notification of the latest information using Twitter automatic search
Trial of writing the configuration file in Python instead of .ini etc.
Check the behavior of destructor in Python
Check the existence of the file with python
Summary of various for statements in Python
The result of installing python in Anaconda
MongoDB for the first time in Python
The basics of running NoxPlayer in Python
Pandas of the beginner, by the beginner, for the beginner [Python]
[Python] Get the character code of the file
[Python3] Understand the basics of file operations
The story of returning to the front line for the first time in 5 years and refactoring Python Django
Python> sys.path> List of strings indicating the path to search for modules
Test & Debug Tips: Create a file of the specified size in Python
The google search console sitemap api client is in webmasters instead of search console
I made a program to check the size of a file in Python
[Implementation example] Read the file line by line with Cython (Python) from the last line
Get the key for the second layer migration of JSON data in python
CERTIFICATE_VERIFY_FAILED in Python 3.6, the official installer for macOS
Output the number of CPU cores in Python
The story of low learning costs for Python
[Python] Sort the list of pathlib.Path in natural sort
The story of creating a store search BOT (AI LINE BOT) for Go To EAT in Chiba Prefecture (1)
Unattended operation of Google Spreadsheets (etc.) in Python
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
Make a copy of the list in Python
Tips for hitting the ATND API in Python
Image processing? The story of starting Python for
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
Search by the value of the instance in the list
Convert the character code of the file with Python3
Speed evaluation of CSV file output in Python
python xlwings: Find the cell in the last row
Basic story of inheritance in Python (for beginners)
Get the update date of the Python memo file.
Periodically notify the processing status of Raspberry Pi with python → Google Spreadsheet → LINE
I want to get the file name, line number, and function name in Python 3.4
Get the last element of the array by splitting the string in Python and PHP