Created
August 11, 2014 07:57
-
-
Save vanechu/9ca16399d103694aff43 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
import html2text | |
import requests | |
from bs4 import BeautifulSoup | |
class Info(object): | |
def __init__(self, words): | |
self.words = words | |
self.url = '''https://www.google.com/search?num=1&newwindow=1&safe=off&es_sm=93&q=define+{query}''' | |
def request(self, word): | |
return requests.get(self.url.format(query=word), verify=True, timeout=2) | |
def process(self, response): | |
soup = BeautifulSoup(response.text) | |
results = soup.select('li[class="g"]') | |
if results and len(results) > 0: | |
return html2text.HTML2Text().handle(unicode(results[0])) | |
else: | |
return "" | |
def query(self): | |
results = [self.process(self.request(word)) for word in self.words] | |
return results | |
if __name__ == "__main__": | |
words = ['iphone','daejeon'] | |
info = Info(words) | |
print info.query() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment