/
knowyourmeme.py
53 lines (40 loc) · 1.84 KB
/
knowyourmeme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import requests
from bs4 import BeautifulSoup
_HEADERS = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'} # defining headers for browser
class KnowYourMeme:
def __init__(self):
self.about = None
self.imageurl = None
self.siteurl = None
self.soup = None
def search(self, term):
for i in term: # change every space to +
if i == " ":
i = "+"
search_url = "http://knowyourmeme.com/memes/" + term # making a url to be used
page = requests.get(search_url, headers=_HEADERS) # requesting code
soup = BeautifulSoup(page.content, 'html.parser') # parsing code with Beautiful Soup
return self.parse(page.content)
def random_image(self):
url = "http://knowyourmeme.com/photos/random"
page = requests.get(url, headers=_HEADERS) # requesting code
return self.parse(page.content)
def parse(self, content):
try:
soup = BeautifulSoup(content, 'html.parser') # parsing it
title = soup.find('meta', attrs={"property": "og:title"})['content'] # finding description
description = soup.find('meta', attrs={"name": "description"})['content'] # finding description
#siteurl = soup.find('meta', attrs={"property": "og:url"})['content'] # finding site url
image = soup.find('meta', attrs={"property": "og:image"})['content'] # finding image url
result = f"{description} \n {image}"
print(result)
return result
except:
result = "No results found for your meme :sob:"
print(result)
return result
#if __name__ == '__main__':
# kym = KnowYourMeme()
# kym.search('pepehands')
# #kym.random_image()