【发布时间】:2019-05-22 21:04:04
【问题描述】:
我是 python 新手。为什么此代码不打印 top50 电影?
#!/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
我当前的输出是空的,在终端上看不到任何东西。
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
我需要这个输出:
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
以此类推,直到 50 岁。
提前致谢。
【问题讨论】:
-
你在没有请求url之前没有得到任何输出,尝试
r = requests.get(output),然后soup = BeautifulSoup(r.text, 'lxml')然后检查soup的内容是网页html。 -
@davedwards 我的输出:
python movie_recom.py Enter you're fav year for movie display: 2018 0%| | 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 -
供将来参考,请编辑您的问题以添加详细信息或说明。特别是由于布局和格式不佳,在 cmets 中代码很难阅读
标签: python beautifulsoup