如何在发送给 Google 的查询之间添加随机延迟以避免在 python 中被阻止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4054254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add random delays between the queries sent to Google to avoid getting blocked in python
提问by Hossein
I have written a program which sends more than 15 queries to Google in each iteration, total iterations is about 50. For testing I have to run this program several times. However, by doing that, after several times, Google blocks me. is there any ways so I can fool google maybe by adding delays between each iteration? Also I have heard that google can actually learn the timesteps. so I need these delays to be random so google cannot find a patter from it to learn my behavior. also it should be short so the whole process doesn't take so much. Does anyone knows something, or can provide me a piece of code in python? Thanks
我编写了一个程序,它在每次迭代中向 Google 发送超过 15 个查询,总迭代次数约为 50。为了进行测试,我必须多次运行该程序。但是,通过这样做,几次之后,Google 阻止了我。有什么方法可以通过在每次迭代之间添加延迟来愚弄谷歌吗?我也听说谷歌实际上可以学习时间步长。所以我需要这些延迟是随机的,所以谷歌无法从中找到一个模式来了解我的行为。它也应该很短,这样整个过程就不会花太多时间。有谁知道一些东西,或者可以给我提供一段python代码?谢谢
采纳答案by nmichaels
First, Google probably are blocking you because they don't like it when you take too many of their resources. The best way to fix this is to slow it down, not delay randomly. Stick a 1 second wait after every request and you'll probably stop having problems.
首先,谷歌可能会阻止你,因为他们不喜欢你占用太多资源。解决此问题的最佳方法是放慢速度,而不是随机延迟。在每个请求后坚持 1 秒钟等待,您可能会停止遇到问题。
That said:
那说:
from random import randint
from time import sleep
sleep(randint(10,100))
will sleep a random number of seconds (between 10 and 100).
将睡眠随机数秒(10 到 100 之间)。
回答by martineau
Since you're not testing Google's speed, figure out some way to simulate it when doing your testing (as @bstpierre suggested in his comment). This should solve your problem and factor its variable response times out at the same time.
由于您不是在测试 Google 的速度,因此在进行测试时想出一些方法来模拟它(正如 @bstpierre 在他的评论中所建议的那样)。这应该可以解决您的问题并同时考虑其可变响应超时。
回答by seriyPS
Also you can try to use few proxy servers for prevent ban by IP adress. urllib support proxies by special constructor parameter, httplib can use proxy too
您也可以尝试使用少量代理服务器来防止被 IP 地址禁止。urllib 通过特殊的构造函数参数支持代理,httplib 也可以使用代理

