album_list = soup.find('ul', {'class': 'm-cvrlst f-cb'}).find_all('li') AttributeError: 'NoneType' object has no attribute 'find_all'
时间: 2023-10-06 19:09:49 浏览: 71
这个错误通常意味着 BeautifulSoup 没有找到指定的 HTML 标签或属性。在这种情况下,它没有找到包含歌曲列表的 `ul` 标签,并返回了一个 `NoneType` 对象,因此在调用 `find_all` 方法时出现了 `'NoneType' object has no attribute 'find_all'` 的错误。
你可以尝试检查你的 HTML 页面,看看是否存在 `ul` 标签,并且它是否具有 `class` 属性为 `m-cvrlst f-cb`。如果不是,你需要更改代码中的相应部分以匹配实际的 HTML 结构。如果是,那么可能是由于页面加载速度太慢或网络连接问题导致 BeautifulSoup 无法正确解析 HTML。你可以尝试使用其他网络库如 requests 或者加入一些等待时间来解决这个问题。
相关问题
import requests from bs4 import BeautifulSoup url = 'https://movie.douban.com/subject/30228394/' header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)\ AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'} response = requests.get(url=url, headers=header) soup = BeautifulSoup(response.text, 'html.parser') tv_infor = {} # 1.获取电视剧名称 name = soup.find(property="v:itemreviewed").string # 根据属性property="v: itemreviewed"查找 tv_infor['name'] = name # 将电影名称加到字典tv_infor中 # 2.获取导演 director = soup.find(rel="v: directedBy").string # 根据属性re1="v:directedBy“查找 tv_infor['director'] = director # 3.获取编剧 soup_list = soup. findAll(class_="attrs")[1].findAll('a') writers = [elem. string for elem in soup_list] tv_infor['writers'] = writers # 4.获取演员 soup_list = soup. findAll(rel="v:starring") actors = [elem. string for elem in soup_list] tv_infor['actors'] = actors # 5.获取类型 soup_list = soup. findAll(property="v: genre") tv_type = [elem. string for elem in soup_list] tv_infor['type'] = tv_type # 6.首播时间 release_date = soup.find(property="v: initialReleaseDate").string tv_infor['release_date'] = release_date # 7.豆瓣评分 rating = soup.find(property="v: average").string tv_infor['rating'] = rating # 8.参评人数 votes = soup.find(property="v: votes").string tv_infor['votes'] = votes print("电视剧《觉醒年代》相关信息如下:") for key, value in tv_infor.items(): print(key, ":", value)运行报错
这段代码是Python 2.x的语法,但在Python 3.x中会出现语法错误。你需要将所有的print语句修改为函数调用的格式,即在print后添加括号,像这样:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://movie.douban.com/subject/30228394/'
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)\ AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'}
response = requests.get(url=url, headers=header)
soup = BeautifulSoup(response.text, 'html.parser')
tv_infor = {}
# 1.获取电视剧名称
name = soup.find(property="v:itemreviewed").string
tv_infor['name'] = name
# 2.获取导演
director = soup.find(rel="v: directedBy").string
tv_infor['director'] = director
# 3.获取编剧
soup_list = soup.findAll(class_="attrs")[1].findAll('a')
writers = [elem.string for elem in soup_list]
tv_infor['writers'] = writers
# 4.获取演员
soup_list = soup.findAll(rel="v:starring")
actors = [elem.string for elem in soup_list]
tv_infor['actors'] = actors
# 5.获取类型
soup_list = soup.findAll(property="v:genre")
tv_type = [elem.string for elem in soup_list]
tv_infor['type'] = tv_type
# 6.首播时间
release_date = soup.find(property="v:initialReleaseDate").string
tv_infor['release_date'] = release_date
# 7.豆瓣评分
rating = soup.find(property="v:average").string
tv_infor['rating'] = rating
# 8.参评人数
votes = soup.find(property="v:votes").string
tv_infor['votes'] = votes
print("电视剧《觉醒年代》相关信息如下:")
for key, value in tv_infor.items():
print(key, ":", value)
```
这样就可以正常运行了。
Traceback (most recent call last): File "D:\python项目\main.py", line 10, in <module> win_data = soup.find("div", class_="win_data").text AttributeError: 'NoneType' object has no attribute 'text'
这个错误信息表明在文件 "D:\python项目\main.py" 第 10 行,在调用 soup.find("div", class_="win_data").text 时发生了 AttributeError 错误。这是因为在 soup.find("div", class_="win_data") 中找不到匹配的元素,返回了 None,而 None 没有 text 属性。
阅读全文