I'm new to python. Why is this code not printing the top50 films?
#!/usr/bin/python3
import requests
from bs4 import BeautifulSoup
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module='bs4')
# website
url = "https://www.imdb.com/search/title?release_date="
year = input("Enter you're fav year for movie display: ")
output = url+year
# extracting the info from website
soup = BeautifulSoup(output, "lxml")
# Display the top 50 films
i = 1
movieList = soup.find_all('div', attrs={'class': 'lister-item mode-advanced'})
for x in movieList:
div = x.find('div', attrs={'class': 'lister-item-content'})
print(str(i) + '.')
header = x.findChild('h', attrs={'class': 'lister-item-header'})
print('Movie: ' + str(header[0].findChild('a'))
[0].contents[0].encode('utf-8').decode('ascii', 'ignore')) #and can someone tell me what is this.. because I’m following some guide. And i didn’t understand this line.
i += 1
My current output is empty, can't see anything on the terminal.
0/50 [00:00<?, ?it/s]1.
Traceback (most recent call last): File "movie_recom.py", line 26, in <module> print('Movie: ' + str((header[0].findChild('a'))
TypeError: 'NoneType' object is not subscriptable
I need this output:
Most Popular Feature Films Released 2018-01-01:
1. Movie: Avengers: Infinity War
2. Movie: Venom
3. Movie: A Quiet Place
4. Movie: Black Panther
5. Movie: I Feel Pretty
6. Movie: Deadpool 2
7. Movie: Ready Player One
8. Movie: Super Troopers 2
9. Movie: Rampage
10. Movie: Den of Thieves
and so on until 50.
Thanks in advance.
You haven't yet issued a request, then you can parse the response content.
This should get the full list:
r = requests.get(output)
soup = BeautifulSoup(r.text, "lxml")
# Display the top 50 films
movieList = soup.find_all('div', attrs={'class': 'lister-item mode-advanced'})
for n, x in enumerate(movieList, 1):
div = x.find('div', attrs={'class': 'lister-item-content'})
print(str(n)+'.', div.find('a', href=True).text)
will return:
1. Aquaman
2. Mowgli: Legend of the Jungle
3. Spider-Man: Into the Spider-Verse
...
50. The Rookie
Thanks Guys for the help but i already solved it.
i = 1
movieList = soup.find_all('div', attrs={'class': 'lister-item mode-advanced'})
for x in tqdm(movieList):
div = x.find('div', attrs={'class': 'lister-item-content'})
# print(str(i) + '.')
header = x.findChild('h3', attrs={'class': 'lister-item-header'})
print(str(i) + '.' + header.findChild('a').text)
i += 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.