python - “str”对象没有属性:BeautifulSoup Python 中的“后代”
问题描述
我正在尝试使用 python 中的 bs4 库获得披萨折扣。但是,无论我尝试什么,我总是得到同样的错误:AttributeError: 'str' object has no attribute 'descendants'
. 有人可以帮我理解我做错了什么吗?
这是我的代码:
from bs4 import BeautifulSoup as soup
import requests
url = requests.get('https://www.papajohns.com/order/specials')
data = url.content
items = soup(data, 'html.parser')
discount_list = soup.find_all('p', {'class': 'description'}).text
for each_item in discount_list:
print(each_item.text)
解决方案
您的代码中有两个错误:
您正在调用作为库而不是第 6 行中的对象beautifulsoup
导入的。您还需要删除@Cz_ 建议的属性。像这样改变这一行:soup
items
.text
discount_list = items.find_all('p', {'class': 'description'})
推荐阅读
- svg - 将 SVG 路径转换为坐标(用于在 Excel 中绘图)
- c# - 存储库模式和 TransactionScope
- javascript - 将产品添加到购物车时,我可以推送数据层吗
- javascript - 尝试使用 D3 在圆圈中心获取文本
- angular - 如何放大 angular 8 中的primeng p-chart?
- angular - Get property value inside of an object that is inside of FormControl in FormGroup and desplay it?
- mysql - 如何从 MySQL 中的另一个表中获取每个特征的相交多边形
- go - 从 Go 中的 StdoutPipe() 读取冻结
- python - 连接到 Firestore 模拟器时 Python firebase_admin 挂起
- postgresql - Docker-compose 应该只运行一次服务