Чтобы получать только тексты [дубликат]Python

Программы на Python
Anonymous
Чтобы получать только тексты [дубликат]

Сообщение Anonymous »

Код: Выделить всё

from pprint import pprint
from bs4 import BeautifulSoup
import requests

html = requests.get ('https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=0&ie=utf8&query=%EB%82%A0%EC%94%A8')
## pprint(html.text)

soup = BeautifulSoup(html.text,'html.parser')
## pprint(soup)

data1 = soup.find('div',{'class':'report_card_wrap'})

data2 = data1.findAll('strong')
data3 = data1.findAll('span')

pprint(data2)
pprint(data3)
В приведенном выше коде я хотел бы выводить только текст. Итак, я попробовал pprint(data2.text) и pprint(data3.text), но выдал ошибку, как показано ниже.

Код: Выделить всё

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()?
Благодарю за любые советы.
Я ожидаю получать только тексты, кроме тегов и классов.

Подробнее здесь: https://stackoverflow.com/questions/790 ... texts-only

Вернуться в «Python»