You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python BeautifulSoup爬取IMDb电影信息遇AttributeError求助

问题解决:AttributeError: ResultSet object has no attribute 'find'

错误原因

你的movies变量是通过find_all('tr')获取到的ResultSet(元素列表),find()方法仅适用于单个BeautifulSoup元素对象,不能直接调用在列表上,这就是报错的核心原因。

解决方案

遍历movies列表中的每一个<tr>元素,对单个元素执行数据提取操作即可。

修正后的完整代码

import requests
from bs4 import BeautifulSoup

url = "https://www.imdb.com/chart/moviemeter/?ref_=nv_mv_mpm.I"
response = requests.get(url)
bs = BeautifulSoup(response.text, 'html.parser')

# 获取所有电影行的列表
movies = bs.find('tbody', class_='lister-list').find_all('tr')

# 遍历每一行提取数据
for movie in movies:
    try:
        title = movie.find('td', class_='titleColumn').a.text
        rating = movie.find('td', class_='ratingColumn imdbRating').strong.text
        year = movie.find('td', class_='titleColumn').span.text.strip('()')
        print(f"电影名称: {title}, 上映年份: {year}, 评分: {rating}")
    except AttributeError:
        # 处理无评分的电影条目
        title = movie.find('td', class_='titleColumn').a.text
        year = movie.find('td', class_='titleColumn').span.text.strip('()')
        print(f"电影名称: {title}, 上映年份: {year}, 评分: 暂无")

补充说明

  • 使用for movie in movies遍历每个电影条目,此时movie是单个<tr>元素,可正常调用find()方法
  • 加入try-except块是因为榜单中部分电影可能未公开评分,避免再次触发AttributeError

内容的提问来源于stack exchange,提问作者Pauline21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 14:50:15