Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
';str';对象没有属性:';后代';在BeautifulSoup Python中_Python_Beautifulsoup_Python Requests - Fatal编程技术网

';str';对象没有属性:';后代';在BeautifulSoup Python中

';str';对象没有属性:';后代';在BeautifulSoup Python中,python,beautifulsoup,python-requests,Python,Beautifulsoup,Python Requests,我正在尝试使用python中的bs4库获得比萨饼折扣。然而,无论我尝试什么,我总是会得到相同的错误:AttributeError:'str'对象没有属性“后代”。有人能帮我理解我做错了什么吗 这是我的密码: from bs4 import BeautifulSoup as soup import requests url = requests.get('https://www.papajohns.com/order/specials') data = url.content items = so

我正在尝试使用python中的bs4库获得比萨饼折扣。然而,无论我尝试什么,我总是会得到相同的错误:
AttributeError:'str'对象没有属性“后代”
。有人能帮我理解我做错了什么吗

这是我的密码:

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)

你做得很好,发短信太多了。你有

discount_list = soup.find_all('p', {'class': 'description'}).text
for each_item in discount_list:
    print(each_item.text)
它有两次
.text
,在第一行和打印中。因此,您实际上是在尝试执行
.text.text
,因为第一个
.text
str
并且没有子体,这就是第二个
.text
的情况,所以您会遇到该错误

要解决此问题,只需将
折扣列表
行更改为

discount_list = soup.find_all('p', {'class': 'description'})

你做得很好,发短信太多了。你有

discount_list = soup.find_all('p', {'class': 'description'}).text
for each_item in discount_list:
    print(each_item.text)
它有两次
.text
,在第一行和打印中。因此,您实际上是在尝试执行
.text.text
,因为第一个
.text
str
并且没有子体,这就是第二个
.text
的情况,所以您会遇到该错误

要解决此问题,只需将
折扣列表
行更改为

discount_list = soup.find_all('p', {'class': 'description'})

一种可能性是,当您对该网页发出url请求时,如果没有特价,它会自动将您重定向到其他网页


我尝试访问该网页,但在没有重定向的情况下无法导航。

一种可能性是,当您对该网页发出url请求时,如果没有特价,它会自动将您重定向到其他网页


我尝试访问该网页,但在没有重定向的情况下无法导航。

您的代码中有两个错误:

您正在调用作为
soup
库导入的
beautifulsou
,而不是第6行中的
items
对象。您还需要删除
.text
属性,如@Cz\uuu。更改此行,如下所示:

discount_list = items.find_all('p', {'class': 'description'})

代码中有两个错误:

您正在调用作为
soup
库导入的
beautifulsou
,而不是第6行中的
items
对象。您还需要删除
.text
属性,如@Cz\uuu。更改此行,如下所示:

discount_list = items.find_all('p', {'class': 'description'})

我也尝试过,但在删除折扣清单声明中的
.text
后,仍然出现相同的错误。啊,是的,请看其他人的答案。如果不是这样的话,那是因为你要做的URL。现代网页中经常充斥着脚本和重定向,直接使用BS4进行解析通常是一件痛苦的事情,甚至是不可能的事情。是的。但还有一个错误。查看我的答案。我也尝试过,但在删除折扣清单声明中的
.text
后仍会出现相同的错误。啊,是的,请查看其他人的答案。如果不是这样的话,那是因为你要做的URL。现代网页中经常充斥着脚本和重定向,直接使用BS4进行解析通常是一件痛苦的事情,甚至是不可能的事情。是的。但还有一个错误。请看我的答案。我想网站坏了