0

всем привет! Нужна помощь по работе супу , мне нужно вытянуть всю информацию которая хранится в тегах a href , без ссылки , просто текст и записать это все в json . Пробовал сам делать все шло коту под хвост( Надеюсь помогут более опытные люди. import requests

from bs4 import BeautifulSoup as bs
import json


def get_html(url):
    r = requests.get(url)
    return r.text




def get_data(html):
    soup = bs(html, 'lxml')
    trs = soup.find('table', {'style':'padding-top:10px;'})




    for rows in trs.find_all('tr')[2:]:
        ras = rows.find_all('div', class_='left-column')
        ras2 = rows.find_all('div', class_='right-column')
        return  rows







def main():
    groups = {
        'T-691': 'https://kbp.by/rasp/timetable/view_beta_tbp/?cat=group&id=26',
        'Т-717': 'https://kbp.by/rasp/timetable/view_beta_tbp/?cat=group&id=62'
    }
    group = input()
    url = 'https://kbp.by/rasp/timetable/view_beta_tbp/?cat=group&id={}'.format(groups[group])
    print(get_data(get_html(url)))


if __name__ == '__main__':
    main()

введите сюда описание изображения

2
  • Пожалуйста замените картинки текстом.
    – Andrey
    25 апр 2019 в 22:53
  • Я прикрепил код к картинке
    – Илья
    25 апр 2019 в 23:02

1 ответ 1

0

Цитата из супной доки (https://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigating-the-tree):

Using a tag name as an attribute will give you only the first tag by that name:

from bs4 import BeautifulSoup

html_doc = """
Текст разбираемой html-странички
"""
soup = BeautifulSoup(html_doc, 'html.parser')

soup.a
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

If you need to get all the tags, or anything more complicated than the first tag with a certain name, you’ll need to use one of the methods described in Searching the tree, such as find_all():

soup.find_all('a')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.