Skip to content

Instantly share code, notes, and snippets.

@vanechu
Created August 11, 2014 07:57
Show Gist options
  • Save vanechu/9ca16399d103694aff43 to your computer and use it in GitHub Desktop.
Save vanechu/9ca16399d103694aff43 to your computer and use it in GitHub Desktop.
# -*- 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