golang 不打印所有 HTML
我正在尝试获取网站的 HTML,但它没有向我显示所有 HTML。我尝试过使用 python,它显示一切正常。然而在 golang 中却没有。
这是我的代码:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
res, err := http.Get("https://golf-land.fr/37-chaussures-de-golf-homme/s-6/marque_2-jordan_golf")
res.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36")
res.Header.Add("Accept", `text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9`)
res.Header.Add("sec-ch-ua", `"Google Chrome";v="93", " Not;A Brand";v="99", "Chromium";v="93"`)
res.Header.Add("sec-ch-ua-platform", "macOS")
res.Header.Add("Accept-Encoding", "gzip, deflate, br")
res.Header.Add("Sec-Fetch-Mode", "navigate")
res.Header.Add("Accept-Language", "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7")
res.Header.Add("Sec-Fetch-Dest", "document")
res.Header.Add("Connection", "keep-alive")
res.Header.Add("Host", "golf-land.fr")
res.Header.Add("Upgrade-Insecure-Requests", "1")
res.Header.Add("Cookie", "_gcl_au=1.1.1697272256.1647946908; _ga=GA1.2.1737361814.1647946908; _fbp=fb.1.1647946908571.1339014446; browserupdateorg=pause; PHPSESSID=pl50pcpu4h09pqqgnf9m22o4p6; PrestaShop-90e2722599b5b00d1f7517fdd071a2e0=def502003ef00f575b7a396d02593a799b93e0f74066c05c63b56b5fcbd384f1510ba541b25f8d0f073b1c4991d8bc13b12ff81cb4523351cdf39309fb8b831dfbe55bb640e0a2e8d39edbe6f9954ddab823574908d702268b54f81366954ec33253bbc900bea125e8ffc4ea63002959b978ed5610615646204f7d84692368943f76616751786883db47300009c6e2044213df9ae64df5ad7987e176819a403ed495b16b170b095719f7bd95af047c268cf761c8e1912650da6aa83bb5d8fefb2e3fc49019c2429a7be1a635941836ad9e52c7ed193fa1a9a7f1dd830016a729b8f204331553b5d326caf38c7d849cd236fce06d001a0d6e914cb4a2e38b81a2f7dc004848f0c16c415618acf13cf9f9332a80ff5edc54d0ad4114465c2ae6241e66f0f24b200b422179127eb3f2476a5285f7820d9cde9e39997479a901aaa98392023a7d5b8ef520628793429437171296f9c9936f0d1d8a00ab35b1894f0694ce75bb9494e18284e49a1fcad08bcbcf4501fcd135f6777fb63684e734c9b23a; _gid=GA1.2.562051467.1648368266")
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln(err)
}
sb := string(body)
fmt.Print(sb)
}
输出中缺少产品行划分,该划分出现在浏览器中或使用 python
I'm trying to get the HTML of a website but it does not show me all the HTML. I have tried using python and it shows everything fine. However it does not in golang.
Here is my code :
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
res, err := http.Get("https://golf-land.fr/37-chaussures-de-golf-homme/s-6/marque_2-jordan_golf")
res.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36")
res.Header.Add("Accept", `text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9`)
res.Header.Add("sec-ch-ua", `"Google Chrome";v="93", " Not;A Brand";v="99", "Chromium";v="93"`)
res.Header.Add("sec-ch-ua-platform", "macOS")
res.Header.Add("Accept-Encoding", "gzip, deflate, br")
res.Header.Add("Sec-Fetch-Mode", "navigate")
res.Header.Add("Accept-Language", "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7")
res.Header.Add("Sec-Fetch-Dest", "document")
res.Header.Add("Connection", "keep-alive")
res.Header.Add("Host", "golf-land.fr")
res.Header.Add("Upgrade-Insecure-Requests", "1")
res.Header.Add("Cookie", "_gcl_au=1.1.1697272256.1647946908; _ga=GA1.2.1737361814.1647946908; _fbp=fb.1.1647946908571.1339014446; browserupdateorg=pause; PHPSESSID=pl50pcpu4h09pqqgnf9m22o4p6; PrestaShop-90e2722599b5b00d1f7517fdd071a2e0=def502003ef00f575b7a396d02593a799b93e0f74066c05c63b56b5fcbd384f1510ba541b25f8d0f073b1c4991d8bc13b12ff81cb4523351cdf39309fb8b831dfbe55bb640e0a2e8d39edbe6f9954ddab823574908d702268b54f81366954ec33253bbc900bea125e8ffc4ea63002959b978ed5610615646204f7d84692368943f76616751786883db47300009c6e2044213df9ae64df5ad7987e176819a403ed495b16b170b095719f7bd95af047c268cf761c8e1912650da6aa83bb5d8fefb2e3fc49019c2429a7be1a635941836ad9e52c7ed193fa1a9a7f1dd830016a729b8f204331553b5d326caf38c7d849cd236fce06d001a0d6e914cb4a2e38b81a2f7dc004848f0c16c415618acf13cf9f9332a80ff5edc54d0ad4114465c2ae6241e66f0f24b200b422179127eb3f2476a5285f7820d9cde9e39997479a901aaa98392023a7d5b8ef520628793429437171296f9c9936f0d1d8a00ab35b1894f0694ce75bb9494e18284e49a1fcad08bcbcf4501fcd135f6777fb63684e734c9b23a; _gid=GA1.2.562051467.1648368266")
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln(err)
}
sb := string(body)
fmt.Print(sb)
}
it is missing the products-row division in the output, which appears in the browser or with python
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http.Get
构造 并发送请求,然后返回服务器对该请求的响应。在发送请求后设置标头(对其响应也同样如此)对已发送的请求本身没有任何影响。您应该使用http.NewRequest
来构造请求,然后设置请求的标头,然后使用(*http.Client).Do
方法。The
http.Get
constructs and sends the request and then returns the server's response to that request. Setting headers after the request was sent (to its response no less) has NO effect on the already sent request itself. You should usehttp.NewRequest
to construct the request, then set the request's headers, and then send it with the(*http.Client).Do
method.