如何在Go中获取DOM HTML

时间:2019-04-22 09:19:36

标签: go goquery

我正在用Go编写解析器HTML。我需要获取HTML并将其传递给另一个函数。

我是这样做的:

  1. 无法将“ doc”传递给其他功能
receivedURL, err := http.Get("http://lavillitacafe.com/")
doc, err := goquery.NewDocumentFromReader(receivedURL.Body)
//"linkScrape" this is another function
contactURL := linkScrape(doc)

  1. HTML已部分转移到另一个功能。
resp, err := http.Get("http://lavillitacafe.com/")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer resp.Body.Close()
    for true {

        bs := make([]byte, 1014)
        n, err := resp.Body.Read(bs)
                contactURL := linkScrape(bs[:n])
        if n == 0 || err != nil{
            break
        }
    }

我该怎么办?

1 个答案:

答案 0 :(得分:0)

这是根据您的用例调整的基本def index if params[:search].present? @topics = Topic.search(params[:search]).paginate(:page => params[:page], :per_page => 5) flash[:notice] = "No records found based on the search." if @topics.blank? else @topics = Topic.all flash[:notice] = "No records found in Database." if @topics.blank? end end 示例:

<div>
    <%= will_paginate @topic, renderer: BootstrapPagination::Rails %>
</end>