2016-04-10 10 views
1

私が叩いているURLからこのタイプの応答が得られます。これを解析して、目的のHTMLを取得する必要があります。GolangでHTTP.GETレスポンスを解析する方法

これは、Ajax =前述したように、

({ "htmlInfo": "Bleh Bleh" "SOME-HTML"、 "otherInfo": "何とか何とか"、 "MOREINFOを"})私は "SOME-HTML"を得るために必要な3つのキーペア値を持っていますが、どうすればそれを得ることができ、 "SOME-HTML"にエスケープ文字があるという主な問題があります。以下はそのような応答の種類です。

u003Cdivクラス= \ u0022container列-2 \

\ u0022 \ u003E \ n \ n \ u003Csectionクラス= \ u0022colメイン\ u0022 \ u003E \ n \ R \ n \ u003Cdivクラス= \ u0027visor-article-リストリストlist-view-recent \ u0027 \ u003E \ r \ n \ u003Cdivクラス= \ u0027grid_itemバイザー記事ティーザーlist_default \ u0027 \ u003E \ n \ u003Ca class = \ u0027grid_img \ u0027 href = \ u0027/manUnited-is-最高\ u0027 \ u003E \ nは\ u003Cimg SRC = \ u0022http://www.xyz.com/sites//files/styles/w400h22

誰もがこの点で私を助けてください。私はこれにどのように取り組むべきか分かりません。

ありがとうございます。

+0

それはあなたを助けるために人々のために簡単ですので、編集して、問題をより明確にしてください。キーと値のペアは何ですか?それはJavascriptですか?どのようにGoでそれを使用していますか? Goのコードと実際の情報は、「SOME HTML」、「Blah Blah」、「Bleh Bleh」だけではありません。 – PieOhPah

答えて

1

最も簡単な方法は、JSONを抽出し、それを構造体に非整列化することです。 \uXXXX部品は、Unicode文字を生成します

package main 

import (
    "encoding/json" 
    "fmt" 
    "regexp" 
) 

// Data follows the structure of the JSON data in the response 
type Data struct { 
    HTMLInfo string `json:"htmlInfo"` 
    OtherInfo string `json:"otherInfo"` 
    MoreInfo string `json:"moreInfo"` 
} 

func main() { 
    // input is an example of the raw response data. It's probably a []byte if 
    // you got it from ioutil.ReadAll(resp.Body) 
    input := []byte(`this=ajax({"htmlInfo":"\u003Cdiv class=\u0022container columns-2\u0022\u003E\n\n \u003Csection class=\u0022col-main\u0022\u003E\n \r\n\u003Cdiv class=\u0027visor-article-list list list-view-recent\u0027 \u003E\r\n\u003Cdiv class=\u0027grid_item visor-article-teaser list_default\u0027 \u003E\n \u003Ca class=\u0027grid_img\u0027 href=\u0027/manUnited-is-the-best\u0027\u003E\n \u003Cimg src=\u0022http://example.com/sites//files/styles/w400h22", "otherInfo": "Blah Blah", "moreInfo": "Bleh Bleh"})`) 

    // First we want to extract the data json using regex with a capture group. 
    dataRegex, err := regexp.Compile("ajax\\((.*)\\)") 
    if err != nil { 
     fmt.Println("regex failed to compile:", err) 
     return 
    } 

    // FindSubmatch should return two matches: 
    // 0: The full match 
    // 1: The contents of the capture group (what we want) 
    matches := dataRegex.FindSubmatch(input) 
    if len(matches) != 2 { 
     fmt.Println("incorrect number of match results:", len(matches)) 
     return 
    } 
    dataJSON := matches[1] 

    // Since the data is in JSON format, we can unmarshal it into a struct. If 
    // you don't care at all about the fields other than "htmlInfo", you can 
    // omit them from the struct. 
    data := &Data{} 
    if err := json.Unmarshal(dataJSON, data); err != nil { 
     fmt.Println("failed to unmarshal data json:", err) 
    } 

    // You now have access to the "htmlInfo" property 
    fmt.Println("HTML INFO:", data.HTMLInfo) 
} 

です:

HTML INFO: <div class="container columns-2"> 

<section class="col-main"> 

<div class='visor-article-list list list-view-recent' > 
<div class='grid_item visor-article-teaser list_default' > 
<a class='grid_img' href='/manUnited-is-the-best'> 
<img src="http://example.com/sites//files/styles/w400h22 
関連する問題