Python requests 多线程抓取 出现HTTPConnectionPool Max retires exceeded异常

bluefog510 2014-05-25 11:50:41
主要代码如下:

import threading
import requests

def get_info():
try:
res = requests.get('http://www.xxx.com/test/json')
if res.status_code == 200 and res.text != '':
print res.text
else:
print res.status_code
time.sleep(10)
get_info()
except Exception as e:
print e

def start():
threads = []
for i in range(40):
threads.append(threading.Thread(target=get_info,args=()))
for t in threads:
time.sleep(0.3)
t.start()
for t in threads:
t.join()

if __name__ == '__main__':
start()


代码临时写,可能有小错误,大概就是这么个意思:
开启40个线程,间隔0.3秒请求。刚开始很正常,但是2轮过后几乎80% 90%的请求都报异常
HTTPConnectionPool(host='http://www.xxx.com/',port=80):Max retries exceeded with url: /test/json (Caused by(class 'socked.error'):[Errno 10060])

请问是哪里出了问题?

我的想法是如果服务器临时封禁的话,应该是报10054的错误。
可看起来又像是服务器封禁,前几轮请求都是很正常的,为什么持续时间越长,抛出的异常就越多?
重试几次的方法我做过,似乎不太起作用:


def get_info(retries=3):
if 200:
...
else:
if retries > 0:
time.sleep(5)
get_info(retries-1)


初学Python,用来作爬虫。实际上这个问题已经困扰我很久了。我想这个应该是在爬虫项目中很常见的问题,请问该如何着手优化(少量异常可以接受)这个问题?
...全文
1747 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
iasky 2014-05-26
  • 打赏
  • 举报
回复
感觉是服务器返回错误了,一般来说还是多模拟几个header,多用代理吧。

37,722

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧