Last active
March 15, 2020 22:48
-
-
Save ncdulo/2d541bc663c8efab157c841a9171fa0d to your computer and use it in GitHub Desktop.
[Python] UrbanDictionary word lookup
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
from bs4 import BeautifulSoup | |
import requests | |
def lookup(word, limit=0): | |
''' | |
Return a list of definitions of 'word' from UrbanDictionary, | |
up to 'limit' results returned. A limit <= 0 will return | |
all results. | |
Raises exception through 'requests' upon HTTP-related errors. | |
TODO: Document which exceptions | |
There is no safety-check on input. Use with caution. Can this even | |
be exploited? Figure that out. | |
''' | |
url = f'https://www.urbandictionary.com/define.php?term={word}' | |
response = requests.get(url) | |
result = [] | |
# If we encounter a 4xx, or 5xx response code something went wrong | |
# Raise an exception. Let the caller determine how to handle it. | |
response.raise_for_status() | |
# Make some soup, look for meaning within ourself. | |
soup = BeautifulSoup(response.text, 'html.parser') | |
results = soup.find_all('div', class_='meaning') | |
# If limit is not a positive integer, return everything. | |
if limit <= 0: | |
limit = len(results) | |
# TODO: This can be greatly cleaned up, the the loop eliminated | |
# by using list slicing, I am almost certain. Make it happen. | |
for index,meaning in enumerate(results): | |
if index >= limit: | |
break | |
result.append(meaning.get_text()) | |
return result | |
# Simple test case. Look up to the top 3 results for Python | |
# Print the results nicely | |
if __name__ == '__main__': | |
urbanize_me = lookup('python', 3) | |
total = len(urbanize_me) | |
for meaning in urbanize_me: | |
print(meaning) | |
print(' - - - -') | |
print(f'Returned {total} results') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment