文章詳情頁
python - 如何解決ConnectionError?
瀏覽:144日期: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. mysql - 如何減少使用或者不用LEFT JOIN查詢?2. html5 - H5 audio 微信端 在IOS上不能播放音樂3. python - 編碼問題求助4. python - 我在使用pip install -r requirements.txt下載時,為什么部分能下載,部分不能下載5. 視頻文件不能播放,怎么辦?6. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處7. mysql - jdbc的問題8. python - Scrapy存在內存泄漏的問題。9. Python爬蟲如何爬取span和span中間的內容并分別存入字典里?10. mysql - 千萬級數據的表,添加unique約束,insert會不會很慢?
排行榜
