Created
March 14, 2014 08:19
-
-
Save Ricky-Wilson/9543932 to your computer and use it in GitHub Desktop.
Scrape word definitions from dictionary.com with python
This file contains hidden or 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
#!/usr/bin/python | |
from bs4 import BeautifulSoup as bs | |
import re | |
from requests import get | |
class dictionary: | |
def remove_non_ascii(self,text): | |
return re.sub(r'[^\x00-\x7F]+','', text) | |
def get_soup(self,url): | |
raw = self.remove_non_ascii(get(url).content) | |
soup = bs(raw) | |
return soup.select("#MainTxt")[0].select('.ds-single')[0].text.strip() | |
def lookup(self,word): | |
base_url = "http://www.thefreedictionary.com/" | |
query_url = base_url + word | |
return self.get_soup(query_url) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment