Python Can';t使用Beauty soup仅打印文本

Python Can';t使用Beauty soup仅打印文本,python,html,python-3.x,text,beautifulsoup,Python,Html,Python 3.x,Text,Beautifulsoup,我正在努力在python3上创建我的第一个项目之一。当我使用以下代码时: def scrape_offers(): r = requests.get("https://www.olx.bg/elektronika/kompyutrni-aksesoari-chasti/aksesoari-chasti/q-1070/?search%5Border%5D=filter_float_price%3Aasc", cookies=all_cookies) soup = Beautiful

我正在努力在python3上创建我的第一个项目之一。当我使用以下代码时:

def scrape_offers():
    r = requests.get("https://www.olx.bg/elektronika/kompyutrni-aksesoari-chasti/aksesoari-chasti/q-1070/?search%5Border%5D=filter_float_price%3Aasc", cookies=all_cookies)
    soup = BeautifulSoup(r.text,"html.parser")
    offers = soup.find_all("div",{'class':'offer-wrapper'})

    for offer in offers:
        offer_name = offer.findChildren("a", {'class':'marginright5 link linkWithHash detailsLink'})
        print(offer_name.text.strip())
我得到以下错误:

Traceback (most recent call last):
  File "scrape_products.py", line 45, in <module>
    scrape_offers()
  File "scrape_products.py", line 40, in scrape_offers
    print(offer_name.text.strip())
  File "/usr/local/lib/python3.7/site-packages/bs4/element.py", line 2128, in __getattr__
    "ResultSet object has no attribute '%s'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
回溯(最近一次呼叫最后一次):
文件“scrape_products.py”,第45行,在
scrape_offers()
文件“scrape_products.py”,第40行,在scrape_offers中
打印(offer_name.text.strip())
文件“/usr/local/lib/python3.7/site packages/bs4/element.py”,第2128行,在__
“ResultSet对象没有属性“%s”。您可能将元素列表视为单个元素。当您打算调用find()时是否调用find_all()?%key?”
AttributeError:ResultSet对象没有属性“text”。您可能将元素列表视为单个元素。当您打算调用find()时,是否调用了find_all()?
我读过很多关于StackOverFlow的类似案例,但我还是情不自禁。如果有人有任何想法,请帮助:)


注意:如果我在没有
的情况下运行代码。text
将显示整个

findchildren返回一个列表。有时会得到一个空列表,有时会得到一个包含一个元素的列表

您应该添加if语句来检查返回列表的长度是否大于1,然后打印文本

import requests
from bs4 import BeautifulSoup
def scrape_offers():
    r = requests.get("https://www.olx.bg/elektronika/kompyutrni-aksesoari-chasti/aksesoari-chasti/q-1070/?search%5Border%5D=filter_float_price%3Aasc")
    soup = BeautifulSoup(r.text,"html.parser")
    offers = soup.find_all("div",{'class':'offer-wrapper'})

    for offer in offers:
        offer_name = offer.findChildren("a", {'class':'marginright5 link linkWithHash detailsLink'})
        if (len(offer_name) >= 1):
            print(offer_name[0].text.strip())

scrape_offers()