F
F
Fox7772021-09-08 19:48:34
beautiful soup
Fox777, 2021-09-08 19:48:34

Why doesn't the text appear?

Here is the code, the problem is that it doesn't want to show the text, why?

import requests
from bs4 import BeautifulSoup

url = requests.get('http://almetpt.ru/2020/site/schedule/group/835')
print(url.status_code)

soup = BeautifulSoup(url.text, "html.parser")
allNews = soup.findAll('div', class_='container')

print(allNews.text)

Traceback (most recent call last):
  File "D:/programmer/PyCharm Community Edition 2020.2.3/bot_parser.py", line 10, in <module>
    print(allNews.text)
  File "D:\programmer\PyCharm Community Edition 2020.2.3\venv\lib\site-packages\bs4\element.py", line 2253, in __getattr__
    raise AttributeError(
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()?

here is the error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Mandzyuk, 2021-09-08
@Ryslan_13

findall() output text with a loop

import requests
from bs4 import BeautifulSoup

url = requests.get('http://almetpt.ru/2020/site/schedule/group/835')
print(url.status_code)

soup = BeautifulSoup(url.text, "html.parser")
allNews = soup.findAll('div', class_='container')

# цикл
for text_all in allNews:
  print(text_all.text)

Can also be done this way
import requests
from bs4 import BeautifulSoup

url = requests.get('http://almetpt.ru/2020/site/schedule/group/835')
print(url.status_code)

soup = BeautifulSoup(url.text, "html.parser")
allNews = soup.findAll('div', class_='container')

print(allNews[0].text)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question