limdef 2021. 12. 28. 11:49

- goquery 사용시 문제가 발생했던 점 기록


- BOM ( byte order mark ) 가 존재하는 경우에,

goquery의 NewDocumentFromReader가 제대로 동작하지 않을 수 있음

 

 

( BOM이란 - 문서 맨 앞에 보이지 않는 특정 바이트를 넣고 이를 이용해 어떤 인코딩 방식이 사용되었는 지 알아내는 방법.

UTF-8의 경우에 BOM이 없는 것이 보통이나 일부 윈도우즈 프로그램은 UTF-8파일을 생성할 때 자동으로 BOM을 넣는 경우도 있다고 함) 

 

 

 

Not working correctly with file contains BOM · Issue #292 · PuerkitoBio/goquery

test.txt file, e := ioutil.ReadFile(`test.txt`) if e != nil { log.Fatal(e) } document, e := goquery.NewDocumentFromReader(bytes.NewReader(file)) if e != nil { log.Fatal(e) } fmt.Println(document.Fi...

github.com

예제 참고

// BOM 포함된 파일 text.txt  
----------------
<html>
<head>
<title>
Some random thing.
</title>
</head>
<body>
<p>
Expected content.
</p>
</body>
</html>

----------------

func main() {
	file, e := ioutil.ReadFile(`test.txt`)
	if e != nil {
		log.Fatal(e)
	}
	document, e := goquery.NewDocumentFromReader(bytes.NewReader(file))
	if e != nil {
		log.Fatal(e)
	}
    
    // head 태그가 파싱되지 않음을 확인
	fmt.Println(document.Find("head").Html())
}


---

출력
 <nil>