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