Python requests多线程抓取请求都报异常无解,pythonrequests,主要代码如下:impor


主要代码如下:

import threadingimport requestsdef 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 edef 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,用来作爬虫。实际上这个问题已经困扰我很久了。我想这个应该是在爬虫项目中很常见的问题,请问该如何着手优化(少量异常可以接受)这个问题?

可能是访问太频繁,被封禁了?

你访问的是小站吧?我遇到过一个站,爬数据有时会出问题,不知道是服务器真力不从心还是被临时封禁了。

因为你本地的端口不够用了。

Python-Requests close http connection

最近我也碰到了这个问题,好像是http连接太多没有关闭导致的,你可以看看这个帖子。
s = requests.session()
s.config['keep_alive'] = False
requests使用了urllib3库,默认的http connection 是keep-alive的,requests中可以设置False关闭。

应该是你的服务器与目标站之间的网络连接出了问题,可以在访问目标站时多重试几次..

编橙之家文章,

评论关闭