Лекция Основы алгоритмизации. Алгоритмы и величины. Этапы решения задачи на ЭВМ



бет37/37
Дата16.01.2022
өлшемі0,96 Mb.
#129365
түріЛекция
1   ...   29   30   31   32   33   34   35   36   37
Байланысты:
Все лекции
Практическая работа Информационный буклет, Сборник Паскаль1, 9 практикалық тапсырма, 3, 5 дәріс, 2-дәріс. Жалпы мағлұматтар, Өмірбаян, 1 лекция, Тәжірибелік жұмыстар (1), test1, 3 тәжірибелік жұмыс ОБП, Вирусные-диареи1, 3 Зертханалық жұмыс, Задание

Извлечение текста из элементов


Теперь, когда мы увидели основные функции модуля etree, давайте попробуем сделать еще несколько интересных вещей с нашими файлами HTML и XML. Почти всегда эти файлы содержат текст между тегами. Итак, давайте посмотрим, как мы можем добавить текст к нашим элементам:

  • # Копирование кода из самого первого примера

  • root = et.Element('html', version="5.0")

  • et.SubElement(root, 'head')

  • et.SubElement(root, 'title', bgcolor="red", fontsize="22")

  • et.SubElement(root, 'body', fontsize="15")



  • # Добавить текст в Elements и SubElements

  • root.text = "This is an HTML file"

  • root[0].text = "This is the head of that file"

  • root[1].text = "This is the title of that file"

  • root[2].text = "This is the body of that file and would contain paragraphs etc"



  • print(et.tostring(root, pretty_print=True).decode("utf-8"))

Результат:

  • This is an HTML fileThis is the head of that fileThis <a href="/ilgermeli-shetel-tili-afilshin-tili-penini-oulifi-english-file.html">is the title of that file</a>This is the body of that file and would contain paragraphs etc This is the body of that file and would contain paragraphs etc "15">This is the body of that file and would contain paragraphs etc

    Достарыңызбен бөлісу:
1   ...   29   30   31   32   33   34   35   36   37




©www.engime.org 2024
әкімшілігінің қараңыз

    Басты бет