|
| 1 | +# Python3 code for movie recommendation based on your emotion |
| 2 | + |
| 3 | +# Import library for web |
| 4 | +# scrapping |
| 5 | +from bs4 import BeautifulSoup as SOUP |
| 6 | +import re |
| 7 | +import requests as HTTP |
| 8 | + |
| 9 | +# Main Function for scraping |
| 10 | + |
| 11 | + |
| 12 | +def main(emotion): |
| 13 | + |
| 14 | + # IMDb Url for Drama genre of |
| 15 | + # movie against emotion Sad |
| 16 | + if(emotion == "Sad"): |
| 17 | + urlhere = 'http://www.imdb.com/search/title?genres=drama&title_type=feature&sort=moviemeter, asc' |
| 18 | + |
| 19 | + # IMDb Url for Musical genre of |
| 20 | + # movie against emotion Disgust |
| 21 | + elif(emotion == "Disgust"): |
| 22 | + urlhere = 'http://www.imdb.com/search/title?genres=musical&title_type=feature&sort=moviemeter, asc' |
| 23 | + |
| 24 | + # IMDb Url for Family genre of |
| 25 | + # movie against emotion Anger |
| 26 | + elif(emotion == "Anger"): |
| 27 | + urlhere = 'http://www.imdb.com/search/title?genres=family&title_type=feature&sort=moviemeter, asc' |
| 28 | + |
| 29 | + # IMDb Url for Thriller genre of |
| 30 | + # movie against emotion Anticipation |
| 31 | + elif(emotion == "Anticipation"): |
| 32 | + urlhere = 'http://www.imdb.com/search/title?genres=thriller&title_type=feature&sort=moviemeter, asc' |
| 33 | + |
| 34 | + # IMDb Url for Sport genre of |
| 35 | + # movie against emotion Fear |
| 36 | + elif(emotion == "Fear"): |
| 37 | + urlhere = 'http://www.imdb.com/search/title?genres=sport&title_type=feature&sort=moviemeter, asc' |
| 38 | + |
| 39 | + # IMDb Url for Thriller genre of |
| 40 | + # movie against emotion Joy |
| 41 | + elif(emotion == "Joy"): |
| 42 | + urlhere = 'http://www.imdb.com/search/title?genres=thriller&title_type=feature&sort=moviemeter, asc' |
| 43 | + |
| 44 | + # IMDb Url for Western genre of |
| 45 | + # movie against emotion Trust |
| 46 | + elif(emotion == "Trust"): |
| 47 | + urlhere = 'http://www.imdb.com/search/title?genres=western&title_type=feature&sort=moviemeter, asc' |
| 48 | + |
| 49 | + # IMDb Url for Film_noir genre of |
| 50 | + # movie against emotion Surprise |
| 51 | + elif(emotion == "Surprise"): |
| 52 | + urlhere = 'http://www.imdb.com/search/title?genres=film_noir&title_type=feature&sort=moviemeter, asc' |
| 53 | + |
| 54 | + # HTTP request to get the data of |
| 55 | + # the whole page |
| 56 | + response = HTTP.get(urlhere) |
| 57 | + data = response.text |
| 58 | + |
| 59 | + # Parsing the data using |
| 60 | + # BeautifulSoup |
| 61 | + soup = SOUP(data, "lxml") |
| 62 | + |
| 63 | + # Extract movie titles from the |
| 64 | + # data using regex |
| 65 | + title = soup.find_all( |
| 66 | + "a", attrs={"href": re.compile(r'\/title\/tt+\d*\/')}) |
| 67 | + return title |
| 68 | + |
| 69 | + |
| 70 | +# Driver Function |
| 71 | +if __name__ == '__main__': |
| 72 | + |
| 73 | + print("Select Your Emotion:\n 1. Anger\n 2. Anticipation\n 3. Disgust\n 4. Fear\n 5. Joy\n 6. Sad\n 7. Surprise\n 8. Trust") |
| 74 | + emotion = input("Enter the emotion: ") |
| 75 | + a = main(emotion) |
| 76 | + count = 0 |
| 77 | + |
| 78 | + if(emotion == "Disgust" or emotion == "Anger" |
| 79 | + or emotion == "Surprise"): |
| 80 | + |
| 81 | + for i in a: |
| 82 | + |
| 83 | + # Splitting each line of the |
| 84 | + # IMDb data to scrape movies |
| 85 | + tmp = str(i).split('>;') |
| 86 | + |
| 87 | + if(len(tmp) == 3): |
| 88 | + print(tmp[1][:-3]) |
| 89 | + |
| 90 | + if(count > 13): |
| 91 | + break |
| 92 | + count += 1 |
| 93 | + else: |
| 94 | + for i in a: |
| 95 | + tmp = str(i).split('>') |
| 96 | + |
| 97 | + if(len(tmp) == 3): |
| 98 | + print(tmp[1][:-3]) |
| 99 | + |
| 100 | + if(count > 11): |
| 101 | + break |
| 102 | + count += 1 |
0 commit comments