python - 请求 - 前几个响应 200 然后 403

标签 python python-3.x web-scraping grequests

我从 Interpol website 收集了通缉犯的链接。大约有 10k 个链接。一项一项地抓取需要几个小时,因此我正在寻找使用 grequests 异步执行此操作的方法。

这是我的链接列表的预览:

final_links[:20]
['https://www.interpol.int/notice/search/wanted/2009-19572',
 'https://www.interpol.int/notice/search/wanted/2015-74196',
 'https://www.interpol.int/notice/search/wanted/2014-37667',
 'https://www.interpol.int/notice/search/wanted/2011-30019',
 'https://www.interpol.int/notice/search/wanted/2009-34171',
 'https://www.interpol.int/notice/search/wanted/2012-334072',
 'https://www.interpol.int/notice/search/wanted/2012-334068',
 'https://www.interpol.int/notice/search/wanted/2012-334070',
 'https://www.interpol.int/notice/search/wanted/2013-26064',
 'https://www.interpol.int/notice/search/wanted/2013-2528',
 'https://www.interpol.int/notice/search/wanted/2014-32597',
 'https://www.interpol.int/notice/search/wanted/2013-23413',
 'https://www.interpol.int/notice/search/wanted/2010-42146',
 'https://www.interpol.int/notice/search/wanted/2015-30555',
 'https://www.interpol.int/notice/search/wanted/2013-2514',
 'https://www.interpol.int/notice/search/wanted/2010-53288',
 'https://www.interpol.int/notice/search/wanted/2015-58805',
 'https://www.interpol.int/notice/search/wanted/2015-58807',
 'https://www.interpol.int/notice/search/wanted/2015-58803',
 'https://www.interpol.int/notice/search/wanted/2015-62307']

现在我正在尝试获取每个链接的响应:

unsent_request = (grequests.get(url) for url in final_links)
results = grequests.map(unsent_request)

前几个结果是响应 200,但大多数(但不是全部)是 403。是否只是国际刑警组织服务器不允许这样做,或者是我做错了什么(我是不是太贪心了?:) )?当我一一处理请求时,它工作得很好。

最佳答案

这很可能是由于他们的网站保护造成的。您本质上是在以编程方式向他们发送垃圾邮件。所以他们让你做一些请求,然后给你一个 403 禁止,因为你是一个坏 child 。您可以简单地检查返回状态代码,然后在 403 上进行短暂的 sleep ,然后再次尝试该请求,每次增加 sleep 时间,直到再次恢复正常。或者,您可以通过 Tor 执行请求,并在收到 403 后继续更改电路以获取新的退出节点。

关于python - 请求 - 前几个响应 200 然后 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42981974/

相关文章:

python - Tkinter - 预加载窗口?

python - 使用 BeautifulSoup 从未知数量的页面中抓取数据

python-3.x - 如何检索桌面快捷方式指向的 url?

python - 在构建这个扭曲的网络抓取工具时我做错了什么?

python - 如果其他单元格包含 'something',如何在 2 个单元格中设置值

python - pandas 计算每个类别的计数和总和组

python-3.x - 如何解释 LDA 组件(使用 sklearn)?

python - r'\' 不应该是 python 中的有效字符串值吗?

python - 在python中使用xpath隔离href内容

ajax - CasperJS 中的 while 循环?