Skip to content

Commit 1b84fa6

Browse files
authored
Add files via upload
1 parent a7e61da commit 1b84fa6

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Docker/dockerfile.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
create a new file as main.py
2+
Add the python code in main.py
3+
4+
Create a docker file
5+
# vim dockerfile
6+
i
7+
8+
FROM python:3.8
9+
ADD main.py .
10+
RUN pip install requests beautifulsoup4
11+
CMD ["python","./main.py"]
12+
13+
:wq!
14+
15+
Build the image
16+
# docker build -t mypy:imdb .
17+
18+
Build a container
19+
docker run -it mypy:imdb
20+
21+
container will open in interactive mode
22+
23+
press y ==> for movie suggestion
24+
25+
press n==> come out of container

Docker/main.py.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import random
2+
import requests
3+
from bs4 import BeautifulSoup
4+
5+
# crawl IMDB Top 250 and randomly select a movie
6+
7+
URL = 'http://www.imdb.com/chart/top'
8+
9+
def main():
10+
response = requests.get(URL)
11+
12+
soup = BeautifulSoup(response.text, 'html.parser')
13+
#soup = BeautifulSoup(response.text, 'lxml') # faster
14+
15+
# print(soup.prettify())
16+
17+
movietags = soup.select('td.titleColumn')
18+
inner_movietags = soup.select('td.titleColumn a')
19+
ratingtags = soup.select('td.posterColumn span[name=ir]')
20+
21+
def get_year(movie_tag):
22+
moviesplit = movie_tag.text.split()
23+
year = moviesplit[-1] # last item
24+
return year
25+
26+
years = [get_year(tag) for tag in movietags]
27+
actors_list =[tag['title'] for tag in inner_movietags] # access attribute 'title'
28+
titles = [tag.text for tag in inner_movietags]
29+
ratings = [float(tag['data-value']) for tag in ratingtags] # access attribute 'data-value'
30+
31+
n_movies = len(titles)
32+
33+
while(True):
34+
idx = random.randrange(0, n_movies)
35+
36+
print(f'{titles[idx]} {years[idx]}, Rating: {ratings[idx]:.1f}, Starring: {actors_list[idx]}')
37+
38+
user_input = input('Do you want another movie (y/[n])? ')
39+
if user_input != 'y':
40+
break
41+
42+
43+
if __name__ == '__main__':
44+
main()

0 commit comments

Comments
 (0)