Python:无法使用BeautifulSoup

Python:无法使用BeautifulSoup,python,beautifulsoup,Python,Beautifulsoup,我是python新手。为什么这个代码不打印前50名的电影 #!/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=" yea

我是python新手。为什么这个代码不打印前50名的电影

#!/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
一直到50岁


提前感谢。

您尚未发出请求,然后可以解析响应内容

这将得到完整的列表:

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

谢谢大家的帮助,但我已经解决了

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

在未向url发出请求之前,如果未获得任何输出,请尝试r=requests.getoutput,然后尝试soup=BeautifulSoupr.text,“lxml”然后检查soup的内容是否为网页html。@davedwards我的输出:python movie_recom.py输入电影显示的最爱年份:2018 0%| | 0/50[00:00为便于将来参考,请编辑您的问题以添加详细信息或澄清。特别是由于布局和格式不佳,注释中的代码很难阅读
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