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)
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