文章详情页
python - 如何解决ConnectionError?
浏览:101日期:2022-06-29 18:02:47
问题描述
Python中用requests模块抓取资料,可以出现我想要的资料,但是资料并不完整,后面会出现ConnectionError,可以怎么解决?我已经设置了Timeout。
我的代码如下:
from pprint import pprintimport requestsimport jsondict_num = dict()for num in range(1,1771): url_api = 'http://api.jisuapi.com/bencao/detail?appkey=bdc8ee0bb0227112&detailid={n}&isdetailed={n}'.format(n=num) r = requests.get (url_api,timeout=60) data = r.json() pprint (data) detail2 = data pprint (detail2)with open ('bencao_detail2', mode='w', encoding='utf8') as file: json.dump(detail2, file)
问题解答
回答1:对方服务器失败你没办法,不过你可以try catch捕捉异常,一段时间后重试
相关文章:
1. docker gitlab 如何git clone?2. 网页爬虫 - Python:爬虫的中文编码问题?3. javascript - 修改表单多选项时和后台同事配合的问题。4. ubuntu 远程管理KVM设置问题5. javascript - vue生成一维码?求助!!!!!急6. css - 手机页面在安卓和苹果浏览器显示不同的小小问题7. javascript - 关于Js中 this的一道题8. angular.js - angularjs如何动态改变ng-model值,在controller中获取input中输入的值并组合post发送到后台9. javascript - H5页面怎么查看console信息?10. browsersync检测的静态页面只能用index.html命名,用demo.html就不能实时同步,检测动态页面的时候,比如wamp环境下,用browsersync能打开页面,但不能实现同步
排行榜
