P
P
ParnishkaSPB2020-06-17 19:25:57
Python
ParnishkaSPB, 2020-06-17 19:25:57

How to get text when parsing?

from bs4 import BeautifulSoup as BS
import requests
import psycopg2
import os


def pages():
    host = 'https:// www. tripadvisor .ru/'(Пробелы поставлены нарочно)
    pages1 = 'Hotels-g298507-oa'
    pages2 = '--St_Petersburg_Northwestern_District-Hotels.html'
    template = '{}{}{}{}'
    countpages = 0
    for h in range(0, 152):
        url = template.format(host, pages1, countpages, pages2)
        responce = requests.get(url).text
        html = BS(responce, 'html.parser')
        for href in html.select('.ui_column'):
            a = href.select("a")
            text = href.find_all('span', class_='text').text
            if len(a) > 0:
                print (a[0].text)
            print (text)
        countpages = (h * 10) * 3

if __name__ == '__main__':
    os.system('clear')
    pages()

There is such a code, an error is generated at the moment with . Mistake:text = href.find_all('span', class_='text').text
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()?
The fact is that on the page in the "text" class there are 2-3 attributes (they stand side by side), I don't know how to pull out only the text from these attributes, I need help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergei Chamkin, 2020-06-17
@ParnishkaSPB

Your find_all returns a list, it has no such method.

You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question