teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

編集

2021/01/13 07:35

投稿

pokerStars
pokerStars

スコア67

title CHANGED
File without changes
body CHANGED
@@ -46,7 +46,7 @@
46
46
  "golang.org/x/text/encoding/japanese"
47
47
  "golang.org/x/text/transform")
48
48
 
49
- func AnytimeDisplayAction(c *gin.Context){
49
+ func DisplayAction(c *gin.Context){
50
50
  const url = "https://ja.wikipedia.org/wiki/SCADA"
51
51
 
52
52
  res, err := http.Get(url)

2

編集

2021/01/13 07:35

投稿

pokerStars
pokerStars

スコア67

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
1
  ```go
2
- func AnytimeDisplayAction(c *gin.Context){
2
+ func DisplayAction(c *gin.Context){
3
3
  const url = "https://ja.wikipedia.org/wiki/SCADA"
4
4
 
5
5
  res, err := http.Get(url)

1

追記

2021/01/13 07:35

投稿

pokerStars
pokerStars

スコア67

title CHANGED
File without changes
body CHANGED
@@ -37,4 +37,36 @@
37
37
 
38
38
  文字化けが発生するので[こちらの記事](https://wireless-network.net/goquery-scraping/)を参考に同じようにコードを書いたのですが変わらず
39
39
  文字化けされてしまいます。goqueryを使う際に上記画像のような文字化けを起こさないようにするにはどうすればよいでしょうか?
40
- 何卒よろしくお願い致します。
40
+ 何卒よろしくお願い致します。
41
+
42
+ **他に試したこと**
43
+ ```go
44
+ import(
45
+ 他省略
46
+ "golang.org/x/text/encoding/japanese"
47
+ "golang.org/x/text/transform")
48
+
49
+ func AnytimeDisplayAction(c *gin.Context){
50
+ const url = "https://ja.wikipedia.org/wiki/SCADA"
51
+
52
+ res, err := http.Get(url)
53
+ if err != nil {
54
+ log.Println(err)
55
+ }
56
+ defer res.Body.Close()
57
+
58
+ utfBody := transform.NewReader(bufio.NewReader(res.Body), japanese.ShiftJIS.NewDecoder())
59
+
60
+
61
+ doc, _ := goquery.NewDocumentFromReader(reader)
62
+ text := doc.Find(".mw-headline").Each(func(i int, s *goquery.Selection) {
63
+ s.Text()
64
+ })
65
+
66
+
67
+ c.HTML(200, "test.html", gin.H{
68
+ "title1": text,
69
+
70
+ })
71
+ }
72
+ ```