Python中用requests模块抓取资料,可以出现我想要的资料,但是资料并不完整,后面会出现ConnectionError,可以怎么解决?我已经设置了Timeout。
我的代码如下:
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)
对方服务器失败你没办法,不过你可以try catch捕捉异常,一段时间后重试