Home  >  Q&A  >  body text

How to solve ConnectionError?

Using the requests module in Python to capture data, the data I want can appear, but the data is incomplete. A ConnectionError will appear later. How can I solve it? I have set Timeout.

My code is as follows:

from pprint import pprint
import requests
import json

dict_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)
女神的闺蜜爱上我女神的闺蜜爱上我2686 days ago922

reply all(1)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-12 09:21:47

    There is nothing you can do if the other party's server fails, but you can try catch to catch the exception and try again after a while

    reply
    0
  • Cancelreply