S
S
SoulHunter0332021-05-29 19:41:09
Python
SoulHunter033, 2021-05-29 19:41:09

Why is find_all not working?

I welcome everyone! ran into a problem while parsing. My code

def parse():
    url = 'kolesakz.html'
    page = open(url, encoding='utf-8')
    soup = BeautifulSoup(page.read(), 'html.parser')
    items = soup.find_all('div', class_ = 'result-block col-sm-8')
    comps = []

    for item in items:
        comps.append({
            'title': item.find_all('span', class_ = 'a-el-info-title').get_text(strip = True)
            })
        for comp in comps:
            print(comp['title'])

parse()`

But an error comes up
Traceback (most recent call last):
  File "C:\Users\90536\Desktop\123\bot.py", line 19, in <module>
    parse()
  File "C:\Users\90536\Desktop\123\bot.py", line 14, in parse
    'title': item.find_all('span', class_ = 'a-el-info-title').get_text(strip = True)
  File "C:\Users\90536\AppData\Local\Programs\Python\Python39\lib\site-packages\bs4\element.py", line 2173, in __getattr__
    raise AttributeError(
AttributeError: ResultSet object has no attribute 'get_text'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?


With exactly the same code, but instead of item.find_all , write item.find , then everything works, but the code returns only one title, and you need all that are on the page. I will be grateful for help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yupiter7575, 2021-05-29
@SoulHunter033

'title': item.find_all('span', class_ = 'a-el-info-title').get_text(strip = True)

find_all returns an array. Array has no get_text method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question