Python requests多线程抓取请求都报异常无解,pythonrequests,主要代码如下:impor
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关闭。
应该是你的服务器与目标站之间的网络连接出了问题,可以在访问目标站时多重试几次..
编橙之家文章,
相关内容
- Python sys.path哪个命令可以代替append,sys.pathappend,如题。
- python3.x版本 通过urlopen打开一个以html结尾的网址 转换成
- python去重元给中有字典项怎么去重,python字典项,在 p
- Python pip install xlwings遇到error找不到原因,pipxlwings,按照
- 《Python编程实践》for语法使用啥意思,,《Python编程实践
- 准备用python做下载器怎么样?需要了解些知识点,pyt
- python urllib2中打印处理信息的方法是什么,pythonurllib2
- 有Python scrapy下载网站数据的源码可以参考吗,pythonsc
- Python爬虫源码求解析问为何for循环语句无法输出,pyt
- 招聘单位对Python和django熟练度的要求是怎样的,python
评论关闭