Created
June 2, 2014 14:14
-
-
Save alangampel/69c3aa02db15c626012f to your computer and use it in GitHub Desktop.
Search with URLS
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') | |
url_str = input('File URL? ') | |
response = urllib.request.urlopen(url_str).read() | |
response_str = response.decode("utf-8") | |
# buffer all text in the file | |
text_buf = '' | |
for line in response_str: | |
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