我一直在尝试将响应加载到 goquery 文档中,但它似乎失败了(尽管它没有引发错误)。
我试图加载的响应来自:
https://www.bbcgoodfood.com/search_api_ajax/search/recipes?sort=created&order=desc&page=4
虽然它不会引发任何错误,但当我调用时,fmt.Println(goquery.OuterHtml(doc.Contents()))
我会得到输出:
<html><head></head><body></body></html>
同时,如果我不尝试将其加载到 goquery 文档中,而是调用
s, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(s))
我得到:
<!doctype html>
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8 no-touch" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9 no-touch" lang="en"> <![endif]-->
<!--[if gt IE 8]> <html class="no-js gt-ie-8 no-touch" lang="en"> <![endif]-->
<!--[if !IE]><!-->
<html class="no-js no-touch" lang="en">
<!--<![endif]-->
<head>
<meta charset="utf-8">
<title>Search | BBC Good Food</title>
<!--[if IE]><![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="prev" href="https://www.bbcgoodfood.com/search/recipes?page=3&sort=created&order=desc" />
<link rel="next" href="https://www.bbcgoodfood.com/search/recipes?page=5&sort=created&order=desc" />
<meta name="robots" content="noindex" />
<style>
.async-hide {
opacity: 0 !important
}
... etc
我正在做的基本逻辑如下:
package main
import (
"fmt"
"net/http"
"github.com/PuerkitoBio/goquery"
"io/ioutil"
)
func main() {
baseUrl := "https://www.bbcgoodfood.com/search_api_ajax/search/recipes?sort=created&order=desc&page="
i := 4
// Make a request
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s%d", baseUrl, i), nil)
// Create a new HTTP client and execute the request
client := &http.Client{}
resp, _ := client.Do(req)
// Print out response
s, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(s))
// Load into goquery doc
doc, _ := goquery.NewDocumentFromReader(resp.Body)
fmt.Println(goquery.OuterHtml(doc.Contents()))
}
完整的回复可以在这里找到。是否有任何特殊原因导致无法加载?