gpt4 book ai didi

python - ResultSet 对象没有属性 'find_all'

转载 作者:行者123 更新时间:2023-12-01 02:06:30 26 4
gpt4 key购买 nike

当我抓取一个网页时,我总是遇到一个问题。

AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

谁能告诉我如何解决这个问题?我的代码如下:

import requests  
r = requests.get('https://www.example.com')
from bs4 import BeautifulSoup
soup = BeautifulSoup(r.text, 'html.parser')
results = soup.find_all('div', attrs={'class':'product-item item-template-0 alternative'})
records = []
for result in results:
name = results.find('div', attrs={'class':'name'}).text
price = results.find('div', attrs={'class':'price'}).text[13:-11]
records.append((name, price,))

我想问一个封闭的问题。如果我想废弃多个页面。如下所示的模式,我使用如下代码,但仍然只废弃第一页,你能解决这个问题吗?

import requests  
for i in range(100):
url = "https://www.example.com/a/a_{}.format(i)"
r = requests.get(url)
from bs4 import BeautifulSoup
soup = BeautifulSoup(r.text, 'html.parser')
results = soup.find_all('div', attrs={'class':'product-item item-template-0 alternative'})

最佳答案

试试这个。您将结果与结果混淆了:

import requests  
r = requests.get('https://www.example.com')
from bs4 import BeautifulSoup
soup = BeautifulSoup(r.text, 'html.parser')
results = soup.find_all('div', attrs={'class':'product-item item-template-0 alternative'})
records = []
for result in results:
name = result.find('div', attrs={'class':'name'}).text # result not results
price = result.find('div', attrs={'class':'price'}).text[13:-11]
records.append((name, price,))

关于python - ResultSet 对象没有属性 'find_all',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49002709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com