python - Too many requests error while crawling users reputation from Stack Overflow
问题描述
I have a list of user ids and I'm interested in crawling their reputation.
I wrote a script using beautifulsoup that crawls users reputation. But the problem is, I get Too many requests error when my script has run for less than a minute. After that, I am unable to open the Stack Overflow manually on browser too.
My question is, how do I crawl the reputation without getting too many request error?
My code is given below:
for id in df['target']:
url='https://stackoverflow.com/users/'+str(id)
print(url)
response=get(url)
html_soup=BeautifulSoup(response.text, 'html.parser')
site_title = html_soup.find("title").contents[0]
if "Page Not Found - Stack Overflow" in site_title:
reputation="NA"
else:
reputation=(html_soup.find(class_='grid--cell fs-title fc-dark')).contents[0].replace(',', "")
print(reputation)
解决方案
我建议使用 Python模块并在你的 for 循环中time
抛出一个。time.sleep(5)
该错误来自您在太短的时间内发出太多请求。不过,您可能不得不玩弄实际的睡眠时间才能让它正确。
推荐阅读
- bash - 格式化 Unix 密码存储 ls 输出
- vb.net - 从 VB.NET 中的文本文件导入数据
- python - 如何在#Telenium 中自动滚动页面。除了拖动方法。该应用程序是在#Kivy 中创建的
- node.js - 如果 URL 不包含特定标头,如何限制对 URL 的访问
- css - 用于引导 4 问题的 Sb 管理模板与侧边菜单
- python - 分离一个链表并计算链表中的每一项
- php - 如何检查工作表是否为空?
- html - 使用Clone Tr后无法使用ID动态元素
- react-native - 即使凭据是对还是错,React Navigation 也会将我从登录屏幕带到主屏幕。此外,没有显示错误警报
- swiftui - 分解@ViewBuilder TupleView