gpt4 book ai didi

go - 错误超时获取HTTP请求golang

转载 作者:IT王子 更新时间:2023-10-29 02:22:21 26 4
gpt4 key购买 nike

我试图从 Reddit 获取 html 源代码使用 Golang:

package main

import (
"fmt"
"io/ioutil"
"net/http"
"time"
)

func main() {
timeout := time.Duration(5 * time.Second)
client := http.Client{
Timeout: timeout,
}
resp, _ := client.Get("https://www.reddit.com/")
bytes, _ := ioutil.ReadAll(resp.Body)
fmt.Println("HTML:\n\n", string(bytes))
defer resp.Body.Close()
var input string
fmt.Scanln(&input)
}

第一次尝试很好。但是第二次就报错了:

<p>we're sorry, but you appear to be a bot and we've seen too many requests
from you lately. we enforce a hard speed limit on requests that appear to come
from bots to prevent abuse.</p>

<p>if you are not a bot but are spoofing one via your browser's user agent
string: please change your user agent string to avoid seeing this message
again.</p>

<p>please wait 6 second(s) and try again.</p>

<p>as a reminder to developers, we recommend that clients make no
more than <a href="http://github.com/reddit/reddit/wiki/API">one
request every two seconds</a> to avoid seeing this message.</p>

我试过设置延迟,但还是不行。抱歉我的英语不好。

最佳答案

Reddit 不希望在他们的站点上出现自动扫描器\抓取器,并且具有机器人保护机制。这是他们的建议:

one request every two seconds

只需在请求之间添加延迟。

关于go - 错误超时获取HTTP请求golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42152964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com