N
N
netz-sanya2017-03-27 21:18:57
go
netz-sanya, 2017-03-27 21:18:57

How to parse html code inside title?

Given the html code, available at the link:

<div name="lesson" class="lesson" title="
<p class=tooltip_name><b>1-1</b></p>
<p class=tooltip_professor>Преп2</p>
<hr />
<p class=tooltip_aud>Практическое занятие</p>
" style="background-color: #D5F6FF;">	
<p>Физ культура</p>
<p class="lesson_aud">ЗАЛ1</p>
</div>

With code and the Goquery library:
teacher, err := td.Find(".lesson").Attr("title") // получает значение title
if err != true {
       log.Fatal(err)
}
teacher= ClearSpace(teacher) // удаляет и обрезает лишние пробелы
fmt.Println(teacher + "\n")

The teacher variable gets:
<p class=tooltip_name><b>1-1</b></p> <p class=tooltip_professor>Преп2</p> <hr /> <p class=tooltip_aud>Практическое занятие</p>

How to get paragraph values?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2017-03-27
@netz-sanya

You just need to parse the html again

package main

import (
  "bytes"
  "fmt"
  "log"

  "github.com/PuerkitoBio/goquery"
)

func main() {
  rawHtml := bytes.NewBuffer([]byte(`
<div name="lesson" class="lesson" title="
<p class=tooltip_name><b>1-1</b></p>
<p class=tooltip_professor>Преп2</p>
<hr />
<p class=tooltip_aud>Практическое занятие</p>
" style="background-color: #D5F6FF;">	
<p>Физ культура</p>
<p class="lesson_aud">ЗАЛ1</p>
</div>
  `))
  doc, err := goquery.NewDocumentFromReader(rawHtml)
  if err != nil {
    log.Fatal(err)
  }

  title, ok := doc.Find(".lesson").Attr("title") // получаем значение title
  if !ok {
    log.Fatal("Cannot find the node with the class 'lesson'")
  }

  // Парсим то, что лежит в title
  docFromTitle, err := goquery.NewDocumentFromReader(bytes.NewBuffer([]byte(title)))
  if err != nil {
    log.Fatal(err)
  }

  // Получаем элемент с именем преподавателя
  teacher := docFromTitle.Find(".tooltip_professor").Text()

  fmt.Println(teacher)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question