首页 > 解决方案 > Python:无法使用 BeautifulSoup

问题描述

我是 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 岁。

提前致谢。

标签: pythonbeautifulsoup

解决方案


您还没有发出请求,那么您可以解析响应内容。

这应该得到完整的列表:

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)

将返回:

1. Aquaman
2. Mowgli: Legend of the Jungle
3. Spider-Man: Into the Spider-Verse
...
50. The Rookie

推荐阅读