Created
June 2, 2014 14:31
-
-
Save alangampel/8c781660774c1f2c2aa8 to your computer and use it in GitHub Desktop.
First attempt to search a local file online
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
import urllib | |
import urllib.request | |
import urllib.parse | |
import json | |
print('\n') | |
print('----------------------------------') | |
print('Searching for internet matches') | |
print('----------------------------------') | |
# open file with greek content | |
print('\n') | |
file_name = input('File name? ') | |
f_greek = open(file_name, 'r') | |
# buffer all text in the file | |
text_buf = '' | |
for line in f_greek: | |
text_buf += line | |
# tokenize | |
tokens = text_buf.split(); | |
print('\n') | |
print('All tokens from file:\n') | |
print(tokens) | |
print('\n') | |
# create string of x tokens | |
token_count_str = input('How many words in search phrase? ') | |
token_count = int(token_count_str) | |
print('\n') | |
i = 0 | |
content_str = '' | |
#while i < token_num: | |
while i < token_count: | |
content_str += tokens[i] + ' ' | |
i += 1 | |
print("Content string is:") | |
print(content_str) | |
url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&" | |
query = urllib.parse.urlencode({'q':content_str}) | |
print("URL query is: ") | |
print('') | |
print(url + query) | |
print('') | |
response = urllib.request.urlopen(url+query).read() | |
json_data = json.loads(response.decode("utf-8")) | |
results = json_data['responseData']['results'] | |
print('\n') | |
indx = 0 | |
for result in results: | |
title = result['title'] | |
url = result['url'] | |
print("JSON result " , indx + 1, ':') | |
print ( title + '; ' + url ) | |
print('') | |
indx += 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment